Removing Blank Spaces from a Group of File Names
This set of commands iterate over each file in the current directory and will replace any blank spaces in a filename with an underscore (_).
First, we’ll create a set of sample files:
$ for i in {0..9}; do touch "file-${i} (number-${i}).txt"; done$ lsfile-0\ (number-0).txt file-4\ (number-4).txt file-8\ (number-8).txtfile-1\ (number-1).txt file-5\ (number-5).txt file-9\ (number-9).txtfile-2\ (number-2).txt file-6\ (number-6).txtfile-3\ (number-3).txt file-7\ (number-7).txt
Remove Parentheses from File Names
This script will rename Windows backup files by removing the ’ (date)’ from the filename. It will remove the parentheses and everything between them from the file name, so that a file with the name file-1 (2023-09-10).txt will be renamed to file-1.txt. Note that since the data between the parentheses will be removed, the resulting file name must be unique for this script to prevent data from being overwritten. Other than that it should work for any fileset that meets the beforementioned constraints.
Processing a List of Files from an Input File
Suppose we have a list of files stored in a text file and we want to perform an operation on each of them within a bash script. How would we go about doing that? Well, there are several options, here are a few.
$ cat input-files.txtfile-1.txtfile-2.txtfile-3.txtfile-4.txtfile-5.txtfile-6.txtfile-7.txtfile-8.txtfile-9.txt
String Processing with Bash
There are various tools built into bash that enable you to manipulate a variable or string which come in handy when writing shell scripts. Here are a few notable ones:
Find the length of a string
${#string}
Get a Substring from a String
${string:pos} or ${string:pos:len}
Removing Substrings from a String
${string#substr}${string%substr}${string##substr}${string%%substr}
Test If a Port is Open with Bash
If netcat isn’t available on your machine and you don’t have the priviledge to install it you can use this trick to test if a port is open or not. It will throw a connection refused message if a port is closed.
$ : </dev/tcp/127.0.0.1/80
And you can use it in a script like this:
(: </dev/tcp/127.0.0.1/80) &>/dev/null && echo "OPEN" || echo "CLOSED"
Command Line Redirection
Redirection is very significant in shell scripting. It provides you a means to save the output of a command to a file or multiple files (one for stdout and one for stderr).
Below is a table of simple redirections that are the most useful in shell scripting. Here we are using the following naming conventions:
stdout– The output of the script/commandstderr– The errors generated by the script/commandoutfile– A target filename where you wish to store the outputerrfile– A target filename where you wish to store the errors
Command Description/Purpose command 2>errfile Redirect stderr to errfile command >outfile 2>errfile Redirect stderr to file named errfile and stdout to file named outfile command &> outfile Redirect stderr and stdout to outfile command 2>&- Just suppress error messages. No file created. No error message displayed on screen command 2>&1 Redirect error messages to standard output. Useful in shell script when you need to forcefully display error messages on screen
Printing Numbers using Thousand Separators
You can use a pipe to awk to output numbers with thousands separators (commas). For Example, here’s how you can total the 5th column of the ls -l command and print it with thousands separators:
$ ls -l | awk '{total = total + $5}END{print total}' | LC_ALL=en_US.UTF-8 awk '{printf("%'"'"'d\n", $0) }'21,387
This can be adapted to other commands as necessary.
Bash Environment Variables
Here are some bash environment variables that are useful to know when you’re using the command prompt:
$0 - name of shell or shell script $1, $2, $3, … - positional parameters to script $# - count of positional parameters $? - exit status of most recent foreground task $- - current options that are set for the shell $$ - PID of the current shell (not subshell) $! - the PID of the most recent background command $DESKTOP_SESSION - path to the current display manager $EDITOR - preferred text editor $LANG - current language $PATH - directory list to search for executables (programs) $PWD - current working directory $SHELL - current shell $USER - current username $HOME - current user’s home directory $HOSTNAME - current name of the host $TERM - current terminal