xargs – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Mon, 24 Oct 2022 05:00:15 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 (Resolved) -bash: /bin/mv: Argument list too long https://tecadmin.net/mv-argument-list-too-long/ https://tecadmin.net/mv-argument-list-too-long/#comments Tue, 23 Nov 2021 02:42:20 +0000 https://tecadmin.net/?p=28334 One of my development servers contains millions of files under a single directory. To free the disk space, we decided to move to them a new folder created on another disk attached to the same system. When tried to move file with mv command, received the following error. -bash: /bin/mv: Argument list too long An [...]

The post (Resolved) -bash: /bin/mv: Argument list too long appeared first on TecAdmin.

]]>
One of my development servers contains millions of files under a single directory. To free the disk space, we decided to move to them a new folder created on another disk attached to the same system. When tried to move file with mv command, received the following error.

-bash: /bin/mv: Argument list too long

An argument list too long is a common problem with a bash that can happen when you have long command line parameters or arguments. You start running a script and it throws errors like “invalid command” or “too long”. The reason for this is that the shell is trying to read past the end of your argument list and there is no end of the input pipe to wait for. A system variable ARG_MAX defines the Maximum Character Length of Arguments In a shell command.

The Solution’s

The quick solution is to use xargs command line utility or find command with -exec … {}. Both commands break a large command into more minor and complete the job without errors.

  • find with xargs

    The following command will move all files with the “.txt” extension to the target directory. Here find will search all files with the “.txt” extension in the current directory as subdirectories. PIPE (|) will take the standard output of the find command and send it to the mv command as standard input. then mv will move files to the target directory one by one.

    find . -name '*.txt' | xargs mv --target-directory=/path/to/dest_dir/ 
    
  • find with exec

    Instead of using xargs, we can also use -the exec command. Here find will search files and exec will execute the mv command for each file one by one and move the file to the destination directory.

    find . -name '*.txt' -exec mv {} /path/to/dest_dir/ \;
    

    The default above commands will navigate recursively to the sub-directories. To limit the find to the current directory only use -maxdepth followed by a limit number to sub-directories.

    find . -name '*.txt' -maxdepth 1 -exec mv {} /path/to/dest_dir/ \;
    

The “Argument list too long” is a common error while handling a large number of files in bash scripts. You can find the max limit with the command getconf ARG_MAX on the shell.

The post (Resolved) -bash: /bin/mv: Argument list too long appeared first on TecAdmin.

]]>
https://tecadmin.net/mv-argument-list-too-long/feed/ 1
(Resolved) – /bin/rm: Argument list too long https://tecadmin.net/rm-argument-list-too-long/ https://tecadmin.net/rm-argument-list-too-long/#comments Wed, 14 Jul 2021 05:56:27 +0000 https://tecadmin.net/?p=26940 One of the directories has millions of files, which is no more required. When trying to delete all files from that folder using the rm command, I got below error message: /bin/rm : Argument list too long If there are a large number of files in a single directory, Then the traditional rm command can [...]

The post (Resolved) – /bin/rm: Argument list too long appeared first on TecAdmin.

]]>
One of the directories has millions of files, which is no more required. When trying to delete all files from that folder using the rm command, I got below error message:

/bin/rm : Argument list too long

If there are a large number of files in a single directory, Then the traditional rm command can not delete all files and ends with an error message Argument list too long.

To resolve this issue and delete all files use xargs command-line utility with the find command.

  1. First change to the directory where all files exists:
    cd dir_contains_file 
    
  2. Then execute command to delete all files in current directory and its sub directories.
    WARNING – This will also delete files from subdirectories. Be careful with this command.

    find . -name '*' | xargs rm 
    

That’s it. you will find all files are deleted now.

The post (Resolved) – /bin/rm: Argument list too long appeared first on TecAdmin.

]]>
https://tecadmin.net/rm-argument-list-too-long/feed/ 2
xargs Command in Linux with Useful Examples https://tecadmin.net/xargs-command-in-linux-with-example/ https://tecadmin.net/xargs-command-in-linux-with-example/#comments Tue, 02 Apr 2013 03:13:23 +0000 https://tecadmin.net/?p=768 xargs is a Linux/Unix powerful command to build and execute command lines from standard input. It takes output of a command and pass it as argument of another command. xargs takes standard input, delimited by blanks or newlines, and executes the command one or more times with any arguments followed by items. Blank lines on [...]

The post xargs Command in Linux with Useful Examples appeared first on TecAdmin.

]]>
xargs is a Linux/Unix powerful command to build and execute command lines from standard input. It takes output of a command and pass it as argument of another command. xargs takes standard input, delimited by blanks or newlines, and executes the command one or more times with any arguments followed by items. Blank lines on the standard input are ignored.

Syntax

xargs [OPTION]... COMMAND [INITIAL-ARGS]...

Here the COMMAND is executed with arguments INITIAL-ARGS and more arguments read from the input.

xargs Command Options

The xargs commands provides a limited number of command line arguments but enough to utilize its features.

  • -0, --null – The items are separated by a null, not whitespace
  • -a, --arg-file=FILE – Read arguments from FILE, not the standard input.
  • -d, --delimiter=CHARACTER – The items in input stream are separated by CHARACTER, not whitespace
  • -E END – Set a string as login EOF. If the string found, the rest will be ignored.
  • -L, --max-lines=MAX-LINES – Specify maximum number of lines (non-blank) to take as input at command line
  • -n, --max-args=MAX-ARGS – Specify max arguments per command line
  • -P, --max-procs=MAX-PROCS – Specify maximum processes to run at a time
  • -p, --interactive – Run processes interactive with prompt before running commands
  • -r, --no-run-if-empty – Do not run command if input arguments are empty.
  • -t, --verbose – Print all commands on screen executing them

xargs Command Examples

Example 1. Copy large number of files to another folder.

Some times we required to copy a long list of files, In that case cp command failed with error “Argument list too long”. We can use xargs to do that task.

find /backup/ -type f | xargs -n1 -i cp {} /var/www/backup/ 
Example 2: Delete multiple files from a folder.

Some times we requird to delete a large number of files from a folder. Below example will delete all .log files from /var/log directory.

find /var/www/tmp/ -type f | xargs rm -f 

Above command will failed to remove files with spaces in named. To handle spaces in xargs command try below command.

find /var/www/tmp/ -type f -print0 | xargs -0 rm -f 
Example 3: Count number of lines in multiple files.

Below example will count number of lines for each .txt file in /opt directory and its subdirectory

find /opt -name "*.txt" | xargs wc -l 

To handle files having spaces in there name, use following command.

find /opt/ -name "*.log" -print0 | xargs -0 wc -l 
Example 4: Make a backup of all configuration files.

If you want to make a backup of all configuration files ( extension .conf ) in your system, use below command.

find / -name "*.conf" |  xargs tar czf  config.tar.gz 
ls -l config.tar.gz 

-rw-r--r--. 1 root root 193310 Apr  1 13:26 config.tar.gz
Example 5. Use custome delimeter with xargs.

We can have also use custom delemeter with xargs command, By default it uses space and new line as delimeter. Use -d parameter to define delimeter.

echo "1,2,3,4,5" | xargs -d, echo 

Output:

1 2 3 4 5
Example 6: Show output in sepreate line with xargs.

In example 5 ouput is showing in single line, We can also specify to show each output in seprate line.

echo "1,2,3,4,5" | xargs -d, -L 1 echo 

Output:

1
2
3
4
5
Example 7: Handling blank space in filenames or path.

To handle spaces in names use -print0 with find command and -0 with xargs command as parameter.

find /tmp -print0 | xargs -0 -L 1 echo 

The post xargs Command in Linux with Useful Examples appeared first on TecAdmin.

]]>
https://tecadmin.net/xargs-command-in-linux-with-example/feed/ 1