xargs | ls | xargs -t -I {} mv {} {}.old | find . -name "*.sh" -print0 | xargs -0 -I {} mv {} ~/back.scripts

To insert file names into the middle of command lines, type:

um@server#ls | xargs  -t  -I  {} mv {} {}.old

This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:

#mv chap1 chap1.old
#mv chap2 chap2.old
#mv chap3 chap3.old

Find all the .mp3 files in the music folder and pass to the ls command, -print0 is required if any filenames contain whitespace.:
   find ./music -name "*.mp3" -print0 | xargs -0 ls

Find all files in the work folder, pass to grep and search for profit:
   find ./work -print | xargs grep "profit"

{} as the argument list marker

{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~/.old.files directory:

$ find . -name "*.sh" -print0 | xargs -0 -I {} mv {} ~/back.scripts

You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
$ find . -name "*.sh" -print0 | xargs -0 -I file mv file ~/back.scripts

Where,
    -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
    -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.



Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all