I have extracted an archive whose contents are not stored in a sub-directory into a directory with filled with other files and is now cluttered and confusing.

To fix this I have extracted the archive into a new empty directory and want to use the directory listing there to delete the files from first directory which got mixed up. The files were originally extracted into the '/usr/src/packages' directory and the new directory is ~/Programs/DBeaver.

vfclists@hp01:~/Programs/DBeaver$ ls -l
total 216
-rw-r--r--  1 vfclists vfclists 48943 Oct 23 23:28 artifacts.xml
drwxr-xr-x  4 vfclists vfclists  4096 Oct 23 23:28 configuration
-rwxr-xr-x  1 vfclists vfclists 79058 Oct 23 22:58 dbeaver
-rw-r--r--  1 vfclists vfclists   206 Oct 23 23:28 dbeaver.ini
-rw-r--r--  1 vfclists vfclists  7919 Oct  9 14:05 dbeaver.png
drwxr-xr-x 23 vfclists vfclists  4096 Oct 23 23:28 features
-rwxr-xr-x  1 vfclists vfclists 35021 Oct 23 22:58 icon.xpm
drwxr-xr-x  2 vfclists vfclists  4096 Oct 23 23:28 licenses
drwxr-xr-x  4 vfclists vfclists  4096 Oct 23 23:28 p2
drwxr-xr-x  6 vfclists vfclists 20480 Oct 23 23:28 plugins
-rw-r--r--  1 vfclists vfclists  1791 Oct 23 23:28 readme.txt

So what I want to do is to pipe the output of ls to xargs but when I run the command the filename gets prepended with a space so I get test output like this.

vfclists@hp01:~/Programs/DBeaver$ ls  | xargs -n 1 echo rm /usr/src/packages/
rm /usr/src/packages/ artifacts.xml
rm /usr/src/packages/ configuration
rm /usr/src/packages/ dbeaver
rm /usr/src/packages/ dbeaver.ini
rm /usr/src/packages/ dbeaver.png
rm /usr/src/packages/ features
rm /usr/src/packages/ icon.xpm
rm /usr/src/packages/ licenses
rm /usr/src/packages/ p2
rm /usr/src/packages/ plugins
rm /usr/src/packages/ readme.txt

There is also the matter of distinguishing between directories and files, but I want to deal with this first.

UPDATE: It turns out that rm -rf works as well on files as on directories, to it doesn't really matter in this instance, so I simply have to replace rm xxxx with rm -rf xxxx

  • rm -rf /usr/src/packages/ artifacts.xml would remove artifacts.xml from the current directory, and the whole of /usr/src/packages/, including /usr/src/packages/artifacts.xml but everything else there too.
    – ilkkachu
    Oct 24, 2016 at 10:39

1 Answer 1


Use -I {}, and {} at the place where you want the argument to appear:

xargs -I {} -n 1 echo rm /usr/src/packages/{}

(You can use something other than {}, {} is just very common.)

Without this, xargs simpy adds the input as additional arguments, so it's not a question of it adding spaces anywhere - the command receives the input as separate arguments.

You probably should use find instead of ls, especially if you want to distinguish files and directories.

Something like this to delete only files:

find . -type f -exec rm /usr/src/packages/{} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.