Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»(Resolved) -bash: /bin/mv: Argument list too long

    (Resolved) -bash: /bin/mv: Argument list too long

    By RahulNovember 23, 20212 Mins ReadUpdated:October 24, 2022

    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.

    Advertisement

    -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.

    command find shell xargs
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Error: EACCES: permission denied, scandir (Resolved)

    How To Install Python 3.11 on Ubuntu 22.04 / 20.04

    Excluding URLs from ProxyPass in Apache

    View 1 Comment

    1 Comment

    1. Indrajeet on December 29, 2021 10:42 am

      Great info. That will be useful for my website.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.