Breaking News

Home / Networking / Using the Linux stat command to create flexible file listings

Using the Linux stat command to create flexible file listings

The stat command supplies a lot of detailed information on files.

It provides not just the date/time of the most recent file changes, but also shows when files were most recently accessed and permissions changed. It tells you the file size in both bytes and blocks. It displays the inode being used by the file along with the file type. It includes the file owner and the associated user group both by name and UID/GID. It displays file permissions in both the “rwx” (referred to as the “human-readable” format) and numerically. On some systems, it might even include the date and time that a file was created (called its “birth”).

In addition to providing all this information, the stat command can also be used to create file listings. These listings are extremely flexible in that you can choose to include any or all of the information described above.

To generate a custom listing, you just need to use the stat command’s -c (or —format) option and specify the fields you want included. For example, to create a listing that shows file permissions in both of the available formats, use this command:

$ stat -c '%n %a %A' my*
my.banner 664 -rw-rw-r--
mydir 775 drwxrwxr-x
myfile 664 -rw-rw-r--
myjunk 777 lrwxrwxrwx
mykey 664 -rw-rw-r--
mylog 664 -rw-rw-r--
myscript 755 -rwxr-xr-x
mytext 664 -rw-rw-r--
mytext.bak 664 -rw-rw-r--
mytwin 50 -rw-r-----
mywords 664 -rw-rw-r--

As you can see in the example above, %n represents the file name, %a the permissions in octal and %A the permissions in the rwx form. A complete list is shown below.

To create an alias for this command, type this or add this definition to your .bashrc file:

Copyright © 2020 IDG Communications, Inc.

Source link

About admin

Check Also

How to use the Apache web server to install and configure a website

If you’ve never deployed a website with the Apache web server, you’re about to learn ...

Leave a Reply

Your email address will not be published. Required fields are marked *