Analyzing file metadata in the shell
Testing Files
Usually, the test
command is not used in full, but rather as a notation using square brackets and matching options:
if [ $? -eq 0 ]; # is the same as if test $? -eq 0;
test
is used to evaluate the type and timestamps of objects in the directory tree. It returns
if the tested condition is true. See Table 3 for test
's options.
Table 3
Test Options
Test | True, if … |
---|---|
-e <Object> |
Object exists |
True, if <Object> exists and … |
|
-b <Object> |
It is a block device file |
-c <Object> |
It is a drawing device file |
-d <Object> |
It is a directory |
-f <Object> |
It is a standard file |
-g <Object> |
The group ID bit is set |
-G <Object> |
The group entries for the process and file match |
-h <Object> |
It is a symbolic link |
-k <Object> |
The sticky bit is set |
-L <Object> |
It is a symbolic link |
-O <Object> |
The query process points to the same owner |
-p <Object> |
It is a FIFO |
-r <Object> |
It is readable |
-s <Object> |
Its size is not 0 |
-S <Object> |
It is a socket |
-u <Object> |
The UID bit is set |
-w <Object> |
It is writable |
-x <Object> |
It is executable |
True, if <Object1> exists and … |
|
<Object1> -ef <Object2> |
<Object2> points to the same object |
<Object1> -nt <Object2> |
Is newer than <Object2> |
<Object1> -ot <Object2> |
Is older than <Object2> |
The test
command is useful for making a distinction in an if
construct. The range of applications is extensive. In a script for saving data, for example, it would be possible to check whether a file named BACKUP.INFO
exists. If so, the script creates a copy of all files that are newer than this file's timestamp. Otherwise, the script creates a full backup.
Listing 3 shows the code for a script that creates a directory named BACKUPTEST
if it doesn't already exist, quickly performing a common task (Figure 12).
Listing 3
tester.sh
#! /bin/bash uvznew () { # FILE? if [ -f BACKUPTEST ]; then read -p "File with same name exists! Rename (y)? " we if [ "$we" = "y" ]; then mv BACKUPTEST BACKUPTEST.file else echo "Either delete or move the BACKUPTEST file!" echo "END OF SCRIPT" fi fi if [ -d BACKUPTEST ]; then cd BACKUPTEST echo -n "The current directory is $PWD" echo " " else echo "Creating BACKUPTEST" mkdir BACKUPTEST fi return 0 } uvznew
Timestamps and Rights
The find
tool not only searches for file and directory names, but it also includes timestamps, access rights, and the file size as a filter if required. See find
's man page to learn about the full scope of this command. For the most important find
options, see Table 4.
Table 4
Find Options
Action | Option | Note |
---|---|---|
-type <type> |
Search by type |
– |
-size +/-<size> |
Search by size |
- = maximum size; + = minimum size; nothing = same size |
-perm <file permissions> |
Search for file permissions |
– |
-newer <files> |
Search for files newer than <file> |
– |
-mtime -/+<N> |
Search for files not modified for <N>days |
+ = at least; - = within |
-atime -/+<N> |
Search for files not accessed for <N> days |
+ = at least, - = within |
-execdir <command> "{}" + |
Execute <command> for found file |
Safer method |
If necessary, you can forward find
's output to a pipe or process it using xargs
, which passes the result to other commands, like tar
. As an example, Listing 4 creates subdirectories for files as a function of their modification date and moves them there. Figure 13 shows the directory's contents before running the script. Figure 14 shows the script in action, and Figure 15 shows the output.
Listing 4
sortme.sh
01 #!/bin/bash 02 # $1 = directory to process 03 if [ -z $1 ]; then 04 echo "No input" 05 exit 06 fi 07 08 cd $1 09 10 for i in $(stat -c %y:%n * | sort -r | tr \ \:); do 11 # Populate variables subdir (subdirectory) 12 # and fn (filename) 13 subdir=$(echo $i | cut -d \: -f1) 14 fn=$(echo $i | cut -d \: -f6) 15 # Do not process if directory 16 # restart loop 17 if [ -d $fn ]; then 18 echo "$fn: Skipping directory" 19 continue 20 fi 21 # Create subdir if needed, and 22 # move file to it 23 if [ -d $subdir ]; then 24 mv -v $fn $subdir 25 else 26 mkdir $subdir 27 mv -v $fn $subdir 28 fi 29 done
The for
loop receives the data courtesy of stat
. Since spaces are used as separators, it replaces them with colons. The output is sorted by date, starting with the newest files.
In the for
loop, the subdir
variable contains the subdirectory to be created and fn
takes the file name. The script evaluates whether or not fn
is a directory and, in this instance, aborts processing. The loop then begins with a new pass. This prevents the script from processing a directory.
If, on the other hand, fn
is a file, the routine then checks again by means of a test whether the subdirectory already exists. If this is not the case, it creates the directory and moves the file to it. If the directory already exists, it simply does the latter. Figure 15 shows a visual overview of tree
.
Conclusions
With the appropriate shell commands, you can identify, evaluate, and change various file and directory metadata. As a result, many operations can be simplified using scripts, which avoid errors and save valuable time.
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.