Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content.
There are many types of files on Linux systems and numerous ways to look at them, from listing permissions to viewing content. You just need to pick the right command to get the information you need. The command to use to get information about a file – especially file content – depends on the type of file that you’re working with. This post describes the most basic commands, as well as some more complicated commands, that will give you information on files and file content.
Permissions and ownership
Listing files on Linux with the ls -l command provides information on the file’s owner and associated group. It also shows you what users can read, modify or execute the file as well as the size of the file (in bytes) and when the file was most recently updated. Here’s an example:
$ ls -l
total 4
lrwxrwxrwx. 1 jdoe jdoe 10 Jan 3 2024 hosts -> /etc/hosts
-rw-rw-rw-. 1 jdoe jdoe 80 Oct 1 12:02 loop
drwxr-xr-x. 1 jdoe jdoe 88 Oct 18 13:20 NOTES
-rw-r--r--. 1 jdoe jdoe 0 Oct 9 13:14 run_stats
The first column of the data above shows which of the read, write and execute permissions are provided to the file owner, the associated group and everyone else (at least every else who has access to the directory). The first character will tell you if what you’re looking at is a regular file (-), a directory (d) or a symbolic link (l) — a file that points to another file.
-rwxr-x---. 1 jdoe jdoe 589 May 2 11:44 shapes3
The rwxr-x— string above shows owner’s (read, write and execute), group’s (read and execute) and everyone else’s (no access) permissions. That dot at the end of the permissions indicates the SELinux security context.
While ls -l is likely the most commonly used ls command, a quick look at the ls command’s man page will show you that there are literally dozens of options available. For example, if you wanted to list the details for a directory rather than its contents, you would use a command like this:
$ ls -ld bin
drwxr-x---. 1 jdoe jdoe 86 Oct 9 13:21 bin
If you want to list files sizes in the “human-readable” format instead of bytes, you would add an “h” to the command as shown in the second command below. In this case, the file size is shown in kilobytes rather than bytes.
$ ls -ld TOPs
-rw-r--r--. 1 jdoe jdoe 9571 Oct 18 12:58 TOPs
$ ls -ldh TOPs
-rw-r--r--. 1 jdoe jdoe 9.4K Oct 18 12:58 TOPs
Viewing file content
The cat, head, tail, more and less commands allow you to view the content of text files. The cat command displays the entire file while head and tail display the first and last lines (defaulting to 10). The commands below display only three lines.
$ head -3 TZs
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
$ tail -3 TZs
W-SU
WET
Zulu
The more command will show you a screenful of lines at a time or fewer if you add an option (e.g., more -10). Both the more and less commands allow you to back up while viewing file content, but less backs up a line at a time while more backs up a screen at a time.
Using the nl command
If you want to view the lines in a text file in a numbered format, you can use the nl command. Here’s an example of using nl along with head to see only the first three lines of the output.
$ nl TZs | head -3
1 Africa/Abidjan
2 Africa/Accra
3 Africa/Addis_Ababa
Viewing other types of files
To view the content of files other than text files while working on the command line, you can use the od -bc command, but it’s going to show you the content in the octal and character (where available) formats shown below. The “E L F” in the output below is short for “executable and linkable format”. This format is used for storing binaries, libraries, core dumps and such.
$ od -bc /usr/bin/more | head -4
0000000 177 105 114 106 002 001 001 000 000 000 000 000 000 000 000 000
177 E L F 002 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0
0000020 003 000 076 000 001 000 000 000 040 070 000 000 000 000 000 000
003 \0 > \0 001 \0 \0 \0 8 \0 \0 \0 \0 \0 \0
The JFIF sequence in the example below identifies the file as being in jpeg format.
$ od -bc hummingbird.jpg | head -6
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 001 000 110
377 330 377 340 \0 020 J F I F \0 001 001 001 \0 H
0000020 000 110 000 000 377 341 032 130 105 170 151 146 000 000 111 111
\0 H \0 \0 377 341 032 X E x i f \0 \0 I I
0000040 052 000 010 000 000 000 005 000 032 001 005 000 001 000 000 000
* \0 \b \0 \0 \0 005 \0 032 001 005 \0 001 \0 \0 \0
In the command below, output from the echo command is piped to the od -bc command to provide an easy example of how this command works. It shows the letters in “hello” followed by a newline character.
$ echo hello | od -bc
0000000 150 145 154 154 157 012
h e l l o \n
Wrap-up
Linux provides very useful options for viewing file attributes (like owners and permissions) as well as file content.