Linux files: creating, listing, updating, and more

Date:


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 ->           <owners>
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      <== set all permissions in one command
$ ls -l myfile
-rw-r--r--+ 1 shs shs 39 Jan  1 00:00 myfile
$ chmod u+x myfile      <== give owner execute permission
$ 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         <== additional permissions for user nemo
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  \0 020   J   F   I   F  \0 001 001 001  \0   H
0000020 000 110 000 000 377 341 070 176 105 170 151 146 000 000 111 111
         \0   H  \0  \0 377 341   8   ~   E   x   i   f  \0  \0   I   I

You can also use the file command to extract descriptive information from a file as in the example below.

$ file camper
camper: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=11, manufacturer=samsung, model=SM-G935V, orientation=upper-left, xresolution=164, yresolution=172, resolutionunit=2, software=GIMP 2.8.18, datetime=2018:04:30 07:56:54, GPS-Data], progressive, precision 8, 3465x2717, components 3

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.

Copyright © 2023 IDG Communications, Inc.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Popular

More like this
Related