How to View Directories and Files in Linux - ls, cd, pwd, cat, more, tail, wc

Working With Directories

You can use various commands to display the current directory, view the contents of a directory, and change directories.

Directory Terms

The following sections describe basic terms used in conjunction with directories.

Directory

A directory is a list of references to objects, which can include files, sub-directories, and symbolic links. Each reference consists of two components: a name and a number. The name of the object is used to identify and access the object. The number specifies the inode in which information about the object is stored.

Inode

An inode is a list of information relating to a particular object (for example, 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 directory that you are currently accessing. To display the current working directory, perform the pwd command.

 $ pwd
/export/home/user1

Displaying the Directory Contents

You can use the ls command to display the contents of a directory. To list the files and directories within the specified directory, perform the ls command without arguments. The syntax for the ls command is:

$ ls -options filename

To list the contents of the user1 directory, perform the ls command.

$ ls
dante dir2 dir5 file.3 file3 fruit2
dante_1 dir3 file.1 file1 file4 practice
dir1 dir4 file.2 file2 fruit tutor.vi

To display the contents of a specific directory within the current working directory, perform the ls command followed by a directory name.

$ ls dir1
coffees   fruit   trees

To display the contents of a directory that is not in the current working directory, perform the ls command with the complete path to that directory.

$ ls /export/home/user1/dir2
beans  notes$

Displaying Hidden Files

Some files are hidden from view when you use the ls command. Hidden files often contain information that customizes your work environment. You can use the ls -a command to list all files in a directory, including hidden files. The file names of hidden files begin with a period (.).

To display hidden files, perform the ls -a command.

$ ls -a
.             dir2          .dtprofile    file2        practice
..            dir3          file.1        file3        .sh_history
dante         dir4          file.2        file4        .solregis
dante_1       dir5          file.3        fruit        .TTauthority
dir1          .dt           file1         fruit2       tutor.vi

A single period (.) represents the current working directory. The double period (..) represents the parent directory, which contains the current working directory.

Displaying a Long List

You can use the ls -l command to view detailed information about the contents of a directory. The output of the ls -l command displays a long listing of file information as Figure 3-3 shows.

linux permission octal information

For a long listing of the contents of the user1 directory, perform the ls -l command, from the user1 directory.

$ ls -l
total 90
-rw-rw----   1 user1    staff       1319 Mar 14 10:12 dante
-rw-rw----   1 user1    staff        368 Mar 14 10:12 dante_1
drwxr-xr-x   5 user1    staff        512 Mar 14 10:12 dir1
drwxr-xr-x   4 user1    staff        512 Mar 14 10:12 dir2
drwxr-xr-x   3 user1    staff        512 Mar 14 10:12 dir3
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 dir4
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 dir5
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.1
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.2
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.3
-rw-rw----   1 user1    staff       1610 Mar 14 10:12 file1
-rw-rw----   1 user1    staff        105 Mar 14 10:12 file2
-rw-rw----   1 user1    staff        218 Mar 14 10:12 file3
-rw-rw----   1 user1    staff        137 Mar 14 10:12 file4
-rw-rw----   1 user1    staff         57 Mar 14 10:12 fruit
-rw-rw----   1 user1    staff         57 Mar 14 10:12 fruit2
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 practice
-rwx--x--x   1 user1    staff      28738 Mar 14 10:12 tutor.vi

To view detailed information on the contents of the dir1 directory, perform the ls -l dir1 command from the user1 directory.

$ ls -l dir1
total 6
drwxr-xr-x   2 user1    staff         512 Feb 22 14:51 coffees
drwxr-xr-x   2 user1    staff         512 Feb 22 14:51 fruit
drwxr-xr-x   2 user1    staff         512 Feb 22 14:51 trees

Displaying Individual Directories

You can use the ls -ld command to view detailed information about a directory without viewing its contents. To obtain detailed directory information for the dir1 directory, perform the ’ls -ld’ command.

$ ls -ld dir1
drwxr-xr-x 5 user1 staff 512 Feb 22 14:51  dir1

Displaying a Recursive List

You can use the ’ls -R’ command to display the contents of a directory and the contents of all of the directory’s subdirectories. This type of list is known as a recursive list.

To view a recursive list of the contents of the dir1 directory, perform the ls -R dir1 command.

$ ls -R dir1
dir1:
coffees  fruit    trees

dir1/coffees:
beans    brands    nuts

dir1/coffees/beans:
beans

dir1/fruit:

dir1/trees:

Displaying File Types

You can use either the ls -F command or the file command to display file types.

Using the ls -F Command

Table below shows the symbols used in the ls -F command output.

Symbol File Type
/ Directory
* Executable
(None) Plain text file or American Standard Code for Information Interchange (ASCII)
@ Symbolic link
$ ls -F
dante      dir2/      dir5/      file.2    file3     fruit2
dante_1    dir3/      file.1     file2     file4     practice/
dir1/      dir4/      file1      file.3    fruit     tutor.vi
$

Using the file Command

You can use the file command to determine certain file types. If you know the file type, you can decide which command or program to use to read the file.

The output from the file command can be 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.

The syntax for the file command is:

file filenames

To view the file type for the dante file, perform the file command and specify the name of the file.

$ file dante
dante:          English text

Changing Directories

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

The syntax for the cd command is:

$ cd directory

When you perform the cd command without options or arguments, the current working directory changes to your home directory. To change directories from the user1 directory to the dir1 directory, perform the cd command:

$ pwd
/export/home/user1
$ cd dir1
$ pwd
/export/home/user1/dir1

Using Path Name Abbreviations

You can use path name abbreviations to easily navigate or refer to directories on the command line. Table 3-2 shows the path name abbreviations.

Symbol Path Name
. Current or working directory
.. Parent directory, the directory directly above the current working directory

To move to the parent directory for dir1, perform the cd .. command.

$ pwd
/export/home/user1/dir1
$ cd ..

Confirm the current working directory by using the pwd command.

$ pwd
/export/home/user1

You can move up multiple levels of the directory hierarchy by using the cd .. command followed by a slash (/).

$ pwd
/export/home/user1
$ cd ../../..
$ pwd
/
$

Moving Around the Directory Hierarchy

You can either use a relative pathname or an absolute pathname to move around the directory hierarchy. A relative pathname lists the directories in the path relative to the current working directory. An absolute pathname lists all the directories in the path, starting with the root (/) directory.

To change directories using a relative path name, perform the cd command with the path name that starts from the current working directory, user1.

$ cd
$ cd dir1
$ pwd /export/home/user1/dir1
$ cd ../dir2
$ pwd
/export/home/user1/dir2
$ cd
$ cd dir1/coffees
$ pwd
/export/home/user1/dir1/coffees

To change directories using an absolute path name, perform the cd command with the complete path name from the root (/) directory.

$ cd
$ cd /export/home/user1/dir1/coffees
$ pwd
/export/home/user1/dir1/coffees
$

Returning to Your Home Directory

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 user1, your home directory would be export/home/user1 or /home/user1.

You can return to your home directory by using two methods:

1. Perform the cd command without arguments.

$ cd
$ pwd
/export/home/user1

2. Perform the cd command with the absolute path name to your home directory:

$ cd /export/home/user1

To navigate to a user’s home directory, perform the cd command with a tilde (~) character in front of the user name. The tilde (~) character is an abbreviation that equates to the absolute path name of the user.

$ cd ~user1
$ pwd
/export/home/user1

You can also use the tilde (~) to represent your home directory in a relative path. The tilde (~) in the following example represents the user1 home directory.

$ cd ~user1
$ pwd
/export/home/user1

You can also use the tilde (~) to represent your home directory in a relative path. The tilde (~) in the following example represents the user1 home directory.

$ cd ~/dir1/fruit

You can also use the tilde (~) character to navigate to another user’s home directory.

$ cd ~user2
$ pwd
/export/home/user2
$ cd
$ pwd
/export/home/user1

Working With Files

There are different commands available that enable you to view file content in a read-only format or to display information about a file. These commands include the cat command, the more command, the tail command, the head command and the wc command.

Viewing Files by Using the cat Command

The cat command displays the contents of one or more text files on the screen. The cat command displays the contents of the entire file without pausing.

$ cat filename

To display the short text file named dante, perform the cat command.

$ cat dante
          The Life and Times of Dante
          by Dante Pocai

Mention “Alighieri” and few may know about whom you are talking. Say “Dante,” instead, and the whole world knows whom you mean. For Dante Alighieri, like Raphael, Michelangelo, Galileo, etc. is usually referred to by his first name. … (output truncated)

NOTE: Do not use the cat command to read binary files. Using the cat command to read binary files can cause a terminal window to freeze. If your terminal window freezes, close the terminal window and open a new terminal window.

Viewing Files by Using the more Command

To view or page through the contents of a long text file, use the more command. The more command displays the contents of a text file one screen at a time. The following message appears at the bottom of each screen.

--More--( n% )

where n% is the percentage of the file that has been displayed. When the –More–( n% ) prompt appears, you can use the keys described in the Table below to scroll through the file.

Scrolling Keys Action
Space bar Moves forward one screen
Return Scrolls one line at a time
b Moves back one screen
h Displays a help menu of features
/string Searches forward for pattern
n Finds the next occurrence of pattern
q Quits and returns to the shell prompt

When the entire file has been displayed, the shell prompt appears. The syntax of the more command is:

$ more filename

To display the first screen of the dante file, perform the more command.

$ more dante
             The Life and Times of Dante
              by Dante Pocai

Mention “Alighieri” and few may know about whom you are talking. Say “Dante,” instead, and the whole world knows whom you mean. For Dante Alighieri, like Raphael, Michelangelo, Galileo, etc. is usually referred to by his first name. There is only one Dante, as we recognize one Raphael, one Michelangelo, and one Galileo.

…. –More–(90%)

Viewing File Content by Using the head Command

The head command displays the first 10 lines of a file. You can change the number of lines displayed by using the -n option. The -n option displays n lines starting at the beginning of the file.

The syntax for the head command is:

$ head -n filename

To display the first six lines of the /usr/dict/words file, perform the head command with the -n option set to 6.

$ head -6 /usr/dict/words
10th
1st
2nd
3rd
4th
5th
$