Linux provides a number of handy commands for managing file permissions, understanding who has access to the files and checking on file content. Credit: Stokkete / Shutterstock There’s a lot more to working with files on Linux than creating, listing and updating them. After all, files can be Linux commands (i.e., binaries), scripts, images, simple text files, pointers to other files or folders. You might remember the “everything is a file” description of Unix and Linux systems. Even sockets and named pipes are files in their own way. Sometimes only the owner can see and use files, sometimes everyone can and sometimes select individuals will also have access. Here are some of the subtleties. Listing files Listing files on Linux is easy. You use the ls command. On the other hand, commands like ls, ls -l, ls -a and ls -ltr work very differently: The ls command with no arguments simply lists file names The ls -l (long listing) command adds file permissions The ls -a command includes file names that start with dots (often referred to as “hidden files”) The ls -ltr command shows files in old-to-new order while ls -lt lists the files in new-to-old order Creating and updating files If you want to create an empty file or update the timestamp on a file, use the touch command. Using touch -a will only change the access time. Using touch -d followed by a date (e.g., touch -d 20230101) will update the timestamp to the date specified: $ ls -l message -rw-rw-r--. 1 shs shs 39 Jun 12 2019 message $ touch -d 20230101 message $ ls -l message -rw-rw-r--. 1 shs shs 39 Jan 1 00:00 message The permissions string displayed for any file can be broken down as follows: The first character represents the file type Characters 2-4 show the owner’s permissions Characters 5-7 show the group’s permissions Characters 8-10 show the permissions for everyone else Character 11 will be a “.” or a “+” (more on this below) The listing below breaks some listings by fields. <- perms -> type own grp oth ex #links own grp date/time file name ==== === === === = = === === ================ ========= - rwx r-- r-- + 1 shs shs 39 Jan 1 00:00 message - rw- r-- r-- . 1 shs shs 425 Sep 19 11:42 5letters d rwx rwx r-x . 4 shs shs 4096 Nov 19 14:46 bin l rwx rwx rwx . 1 shs shs 13 Mar 23 2020 www -> /var/www/html The file type will most often be shown as a hypen (–) meaning it’s a regular file, but this doesn’t distinguish a text file from an image, etc. A “d” means it’s a directory and an “l” means that it’s a symbolic link. In fact, if you use a command like this one, you can count how many of each file type you have in the current directory. $ ls -l | cut -c-1 | sort | grep -v t | uniq -c 970 - 88 d 17 l I inserted the grep -v t command so as not to include the final “total” (the file count) that is added by the sort command. Changing file permissions and ownership You can use the chmod (change file owner) command to change file permissions. The command allows you to change all permissions in one command or to add or remove individual permissions as shown in the examples below. $ chmod 644 myfile $ ls -l myfile -rw-r--r--+ 1 shs shs 39 Jan 1 00:00 myfile $ chmod u+x myfile $ ls -l myfile -rwxr--r--+ 1 shs shs 39 Jan 1 00:00 myfile Keep in mind when you use the chmod command that “u” means “user” (owner), “g” means “group”, and “o” means “other” (everyone else). The chown command requires sudo access even if the file you are trying to change belongs to you. $ sudo chown nemo myfile Looking at file permissions–who can do what The normal long listing for a file will display the owner and group and the permissions assigned to each as well as the permissions given to anyone else. In the examples above using the “myfile” file, the owner and group are both “shs”. This means only this single user (shs is the only member of the shs group) has write access, but any user can read the file. If the file looks like the below, on the other hand, you need to dig a little further to figure out who else might be able to view the file’s content: $ ls -l notes -rw-rw-r--+ 1 shs shs 3068 Dec 21 2018 notes That + sign at the end of the permissions string indicates that additional permissions have been set up besides the normal owner, group, and everybody else. To see the details, use the getfacl command like this: $ getfacl notes # file: zipfiles # owner: shs # group: shs user::rw- user:nemo:rwx group::rw- mask::rwx other::r— This command shows us that nemo also has read, write and execute permissions. This type of additional access to files can be provided to individuals or groups using the setfacl command as shown in the examples below. The first of these commands allows nemo to have read, write, and execute permissions without being a member of the group associated with the file. The second command gives all members of the wheel group read and write access. $ setfacl -m u:nemo:rwx notes $ setfacl -m g:wheel:rw message NOTE: The -m means “modify”, the u stands for “user”, and the g means “group”. Looking at file content Commands to view the content of files depend on the type of file you want to view. The cat command allows you to display the content of text files and this, of course, includes source code, .bashrc files and such. Image files can be displayed on the desktop, but in a terminal window, you can only examine content by dumping the file in octal or other formats. This od -bc command shows us that the file in question is a jpg file – even if it lacks a proper file extension because it pulls up identifying data from the file contents. $ od -bc camper | head -4 0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 001 000 110 377 330 377 340 Wrap-Up There are a lot of useful commands for working with files. They allow you to view and control who has access to files and how they can use them. They also allow you to change settings as needed. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux news High-bandwidth memory nearly sold out until 2026 While it might be tempting to blame Nvidia for the shortage of HBM, it’s not alone in driving high-performance computing and demand for the memory HPC requires. By Andy Patrizio May 13, 2024 3 mins CPUs and Processors High-Performance Computing Data Center opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to Download our SASE and SSE enterprise buyer’s guide From the editors of Network World, this enterprise buyer’s guide helps network and security IT staff understand what Secure Access Service Edge (SASE) and Secure Service Edge) SSE can do for their organizations and how to choose the right solut By Neal Weinberg May 13, 2024 1 min SASE Remote Access Security Network Security PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe