This post explains how to work with files and directories. This includes tasks, such as locating your position in the directory structure, viewing file contents, copying and moving files and directories, creating and removing files and directories, and searching for files and directories.
A directory is a list of references to objects, which can include files, subdirectories, and symbolic links. Each reference consists of two components:
- A name: The name of the object is used to identify and access the object.
- A number: The number specifies the inode in which information about the object is stored.
You can use various commands to display the current directory, view content of a directory, and change directories.
What is an Inode?
An inode is a list of information relating to a particular object, such as a file, directory, or symbolic link. The information held by the inode includes the type of object about which the inode holds information, permissions, ownership information, and the locations in which data is stored.
Determining the Current Directory
The pwd command identifies the full or absolute path name of the current working directory.
$ pwd /home/user
Displaying the Directory Content
The ls command displays the content of a directory. The syntax for the ls command is:
$ ls [-options] [filename]
Below are few examples of using ls command to list the directory contents.
1. To list the files and directories in the current directory (/export/home/student), enter the ls command without arguments.
$ ls file1 file2 file3
2. To display the content of a specific directory within the current working directory, enter the ls command followed by the directory name.
$ ls dir1 textfile1 textfile2 textfile3
3. To display the content of a directory that is not in the current working directory, enter the ls command with the complete path to that directory.
$ ls /var/tmp/test test1 test2 test3
Displaying the Directory Content With Options
The ls -l command displays a long listing of file information.
The following is a brief explanation of the parts of the long list displayed in the figure above:
- The first character is the file type. It can be a file(-) or directory(d) or symbolic link(l) etc
- The second nine places indicate the file permissions: r means readable, w means writable, x means executable, and the – means denied.
- The third section (one number) is the link count.
- The fourth section is the owner (user).
- The fifth section is the group (user).
- The sixth section is the file size.
- The seventh section is the date.
- The eighth section is the file name.
Some files are hidden from view when you use the ls command. Hidden files often contain information that customizes your work environment. The “ls -la” command lists all files in a directory, including hidden files.
$ ls -la total 1296 dr-xr-x---. 8 root root 4096 May 12 15:25 . drwxr-xr-x. 23 root root 4096 May 12 15:25 .. -rw-r--r-- 1 root root 139 Apr 14 13:01 a -rw-r--r-- 1 root root 3564 May 12 12:53 .bash_history -rw-r--r-- 1 root root 18 Mar 12 21:32 .bash_logout -rw-r--r-- 1 root root 176 Mar 12 21:32 .bash_profile -rw-r--r-- 1 root root 176 Mar 12 21:32 .bashrc
The ls -ld command displays detailed information about a directory without showing its content.
$ ls -ld directory name
For example, to obtain detailed directory information for the dir1 directory, enter the ls -ld command.
$ ls -ld dir1 drwxr-xr-x 2 root root 4096 May 12 15:32 dir1
For example, to view a recursive list of the content of the dir1 directory, enter the ls -R dir1 command.
$ ls -R dir1 dir1: file1 file2 file3 testdir dir1/testdir: textfile1 textfile2 textfile3
Displaying File Types
Knowing the file type may help you decide the command or program to use for reading the file. The ls -F command or the file command displays the file types. For example:
$ ls -F dir1/ dir2/ dir3/ file1 file2 file3
The following table shows the symbols or indicators used with the “ls –F” command output.
||||First In First Out (FIFO)|
The file command also helps determine certain file types. The syntax for the file command is:
$ file filename
For example, to view the file type of the file “test”, enter the file command and specify the name of the file.
$ file test test: ASCII text
The output from the file command is one of the following:
- Text: Text files include American Standard Code for Information Interchange (ASCII) text, English text, command text, and executable shell scripts.
- Data: Data files are created by programs. The file command indicates the type of data file, such as a FrameMaker document, if the type is known. The file command indicates that the file is a data file if the type is unknown.
- Executable or binary: Executable files include 32-bit executable and extensible linking format (ELF) code files and other dynamically linked executable files. Executable files are commands or programs.
When working within the directory hierarchy, you always have a current working directory. When you initially log in to the system, the current directory is set to your home directory. You can change your current working directory at any time by using the cd command. For example, to change directories from the student directory to the dir1 directory, use the cd command:
$ pwd /home/user $ cd dir1 $ pwd /home/user/dir1 $
When you use the cd command without options or arguments, the current working directory changes to your home directory. In the command line, you can use path name abbreviations to easily navigate to or refer to directories. The table describes the path name abbreviations.
|.||Current or working directory|
|..||Parent directory, the directory directly above the current working directory|
For example, to move to the parent directory for dir1, enter the cd .. command.
$ pwd /home/user/dir1 $ cd ..
Confirm the current working directory by using the pwd command.
$ pwd /home/user
Relative and Absolute Path Name
You can use either a relative or an absolute path name to move around the directory hierarchy. A relative path name lists the directories in the path relative to the current working directory. An absolute path name lists all the directories in the path, starting with the root (/) directory.
For example, to change directories using a relative path name, enter the cd command with the path name that starts from the current working directory, student.
$ cd $ cd dir1 $ pwd /home/user/dir1
$ cd ../dir2 $ pwd /home/user/dir2
$ cd $ cd dir1/coffee $ pwd /home/user/dir1/coffee
For instance, to change directories using an absolute path name, enter the cd command with the complete path name from the root (/) directory.
$ cd $ cd /home/user/dir1/coffee $ pwd /home/user/dir1/coffee
The home directory of a regular user is where the user is placed after logging in. The user can create and store files in the home directory. Often the name of a user’s home directory is the same as the user’s login name. For example, if your user login name is john, your home directory would be /home/john.
Returning to Your Home Directory
You can return to your home directory by using one of the two methods:
1. Use the cd command without arguments.
$ cd $ pwd /home/user
2. Use the cd command with the absolute path name to your home directory.
$ cd /home/user
You can also navigate to a user’s home directory, using the cd command with a tilde (~) character in front of the username. The tilde (~) character is an abbreviation that equates to the absolute path name of the user.
$ cd ~user $ pwd /home/user
Note: The tilde (~) character is a shell facility and is not available in all shells.
You can also use the tilde (~) character to represent your home directory in a relative path. The tilde (~) in the following example represents the student home directory.
$ cd ~/dir1/coffee
You can also use the tilde (~) character to navigate to another user’s home directory.
$ cd ~user2 $ pwd /home/user2 $ cd $ pwd /home/user1