Replace Strings in Multiple Files

“sed” command can be used for replacing strings in a file. For example, you can use the command below to replace “Apple” with “Orange” in fruits.txt file.

$ sed -i".bkp" -e "s/Apple/Orange/g" fruits.txt

Parameter “-i” makes the target file overwritten, but a backup is created by specifying a string (“.bkp” for this case) after “-i” whose filename has the string as suffix.

You can combine “xargs” and “find” command with “sed” to process multiple files at once. “find” is for searching files and “xargs” is for executing any commands using values from standard input as parameters. For example, you can use the command below to replace “Apple” with “Orange” in all files in current directory.

$ find . -maxdepth 1 -type f | xargs sed -i".bkp" -e "s/Apple/Orange/g"

Remove Old Files

You can use “find” command with “-exec” parameter to remove old files in UNIX system.

$ find <Directory> -maxdepth 1 -mtime +<Days> -exec rm -f {} \;

Please see the following for the meanings of each parameter.

<Directory> Specify a directory where the target files are located.
-maxdepth 1 Search files only in the given directory by setting “1”. Ignore the child directories.
-mtime +<Days>

Search files modified more than <Days> days ago.

 

-exec rm -f {} \; Execute “rm” command on the found files.

 

Continue Job in Background after Logout

In order to continue a job after logout, we can use “nohup” command which ignores HUP signal.

$ nohup command

Adding “&” gets the command to run in background.

$ nohup command &

Standard output and error of the command are output to nohup.out file created in the directory where the command is executed. If you want to avoid the file creation, redirect standard error to standard output and standard output to /dev/null.

$ nohup command > /dev/null 2>&1 &

Usually “nohup” is used in this style.

Top