Americas

  • United States
sandra_henrystocker
Unix Dweeb

Four ways to view files and file permissions on Linux

How-To
May 08, 20234 mins
Linux

Viewing the content of files and examining access permissions and such are very different options. This post examines a number of ways to look at files on Linux.

Files

There are a number of ways to view files on Linux, because, after all, files on Linux are multifaceted. They have names, they have content, they have access permissions, and they have dates and times associated with their “birth” (when they were initially added to the file system) as well as when they were last changed and last accessed. This post covers the commands that allow you to view all these details.

Listing files

The easiest and most obvious way to list files is with the ls command. By default, ls will list files in the current directory in name order, but you can reverse that by adding the -r option.

$ ls | head -3
4letters
4_vowel_words
4Vwords
$ ls -r | head -3
zoom-mtg
zodiac
zipfiles.bat

You can list files in a tree-like fashion by using the tree command. It will display the directory structure in a way that clearly indicates the directory levels.

$ tree
.
│   ├── xtra
│   │   ├── date
│   │   ├── doit
│   │   ├── dt
│   │   ├── ex
│   │   ├── loop

More information on listing and viewing files are available at these pages:

Viewing access permissions

The ls command with the -l option displays permissions in the “rwx” fashion. By adding “a” to the options, it will include files and directories that start with a “,” (otherwise omitted).

$ ls -l | head -5
total 120528
-rw-r-----.  1 shs  shs      66040 Jul  5  2022 4letters
-rw-r-----.  1 shs  shs        174 Jul  8  2022 4_vowel_words
-rw-r-----.  1 shs  shs        174 Jul  8  2022 4Vwords
-rw-r-----+  1 shs  shs     131310 Sep 19  2022 5letters
$ ls -al | head -5
total 133480
drwxr-xr-x. 103 shs  shs      36864 May  6 11:43 .
drwxr-xr-x.  25 root root      4096 Feb 17 11:36 ..
-rw-r-----.   1 shs  shs      66040 Jul  5  2022 4letters
-rw-r-----.   1 shs  shs        174 Jul  8  2022 4_vowel_words

You can use the getfacl command to include additional file access permissions.

$ getfacl 5letters
# file: 5letters
# owner: shs
# group: shs
user::rw-
user:popeye:r--        

Notice that the command output above shows one user (popeye) who has read access to the file without being a member of the associated group or the file having access permission for everyone.

More information on Linux permissions is available at these pages:

Examining file content

The cat, head and tail commands allow you to view file content. While cat will display the entire contents, you can pass the output to the more command to view a screenful at a time. The head and tail commands display lines at the top and bottom of text files.

$ cat alert
REMINDER: This server will be shutting down @ 6PM tonight. Please finish your
        work by 5:45 and log off.
$ cat 5letters | more
aahed
aalii
aalst
aalto
aamsi
…

$ head -5 4letters
1080
10th
AAAA
AAAL
AAAS

$ tail -5 4letters
zuza
ZWEI
zyga
zyme
Zysk

You can use the grep command to pick out lines that contain some content that you are looking for and nothing else. The grep command below displays only lines that begin with a “z”.

$ grep ^z 4letters | tail -5
zulu
zuni
zuza
zyga
zyme

More coverage on the head and tail commands can be viewed in this head and tail commands video.

The od command will display file contents in a very different fashion. It displays the characters along with their octal values – useful for troubleshooting.

$ od -bc alert
0000000 122 105 115 111 116 104 105 122 072 040 124 150 151 163 040 163
          R   E   M   I   N   D   E   R   :       T   h   i   s       s
0000020 145 162 166 145 162 040 167 151 154 154 040 142 145 040 163 150
          e   r   v   e   r       w   i   l   l       b   e       s   h
0000040 165 164 164 151 156 147 040 144 157 167 156 040 100 040 066 120
          u   t   t   i   n   g       d   o   w   n       @       6   P
0000060 115 040 164 157 156 151 147 150 164 056 040 120 154 145 141 163
          M       t   o   n   i   g   h   t   .       P   l   e   a   s
0000100 145 040 146 151 156 151 163 150 040 171 157 165 162 012 011 167
          e       f   i   n   i   s   h       y   o   u   r  n  t   w
0000120 157 162 153 040 142 171 040 065 072 064 065 040 141 156 144 040
          o   r   k       b   y       5   :   4   5       a   n   d
0000140 154 157 147 040 157 146 146 056 012
          l   o   g       o   f   f   .  n
0000151

More details on the grep command are available at The many faces of grep

Viewing file access times

The stat command displays the date of a file’s “birth” and when it was last changed and last accessed.

$ stat 4letters
  File: 4letters
  Size: 66040           Blocks: 136        IO Block: 4096   regular file
Device: 8,17    Inode: 3015994     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/     shs)   Gid: ( 1000/     shs)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2023-05-06 12:14:49.279689941 -0400
Modify: 2022-07-05 10:06:54.798552673 -0400
Change: 2022-07-05 10:06:54.798552673 -0400
 Birth: 2022-07-05 10:06:40.039362108 -0400

The date -r command can also be used to show a file’s last modification time, though it does so in a different format than the stat command.

$ date -r 4letters
Tue Jul  5 10:06:54 AM EDT 2022

Wrap-up

A handy variety of Linux commands can help you see anything about files that you want to see. Try the commands in this post and you may find they they’re a lot more useful than you expected.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.