2

I am still pretty new to terminal based commands but while I was doing a rsync command between remote and local, I have accidentally 'copied' the files to the wrong place (Eg. /serA/items) instead of /serA/items/count01

And so, currently the /items directory consists of the count01 folder as well as several other items that was the same contents as the ones in count01

Is there a quick way to delete all these wrong copied items (there are different file formats) without touching the count01 folder? While i can use 'rm -f ' but it is a tedious process as there are over 100 plus files...

Also I am unable to delete the files manually as they are under someone else name and the permission seems to be locked.

4
find . -maxdepth 1 -type f -delete

Though if you don't have permissions to delete them, this won't work.

  • Your command works after adding in sudo. Thank you. – dissidia Sep 4 '15 at 2:27
  • -type f is for regular files only, it's not the same as ! -type d (any file type but directory). – Stéphane Chazelas Aug 22 '17 at 14:27
  • True. On find -delete invocations I habitually scope the predicates as narrowly as possible; you can always delete leftovers, but you can't undelete false positives. In this case, I guess it depends on what if anything you're doing with symlinks; other types would seem rare. – Tom Hunt Aug 23 '17 at 20:02
3

You have a bunch of files and folders:

$ ls
count01  file_a  file_b  file_c  folder_a  folder_b  folder_c

To delete all except count01:

$ rm -r !(count01)
$ ls
count01
  • 1
    Tried yours but got this error instead - Badly placed ()'s... – dissidia Sep 4 '15 at 2:00
  • Can you tell me what shell you are using by posting the output of $ echo SHELL? – user1717828 Sep 4 '15 at 10:37
0

I recommand to use pipline commands as follow:

find "actual/path" -type f | grep -v "actual/path/plus/foler/to/conserve" |xargs rm

first part : find all files in the actual directory with the -type f

second part : exclude your path you need to conserve with the -v

last part : remove all files responding from first part and second part

In short you could simply do :

cd
find . -type f | grep -v /serA/items/count01 |xargs rm

with this command all files and folder included in your path is conserved That's just awsome !!

  • This will break with files containing spaces (pretty common these days), and break badly with files that contain newlines (not common but quite possible). Also, if a manual delete fails (per last sentence of the question) why would this find ... | xargs rm work any better? – roaima Aug 22 '17 at 16:52
  • About the spaces the quote is your friend, just use it. I recommand to safely make your command. make a cd in your repo, then make a cd command + the first part of your folder name and then use tab to have the end of your path. You would have special caracteres "\" as linux normal writing convention. Be sure to have the good command before any acion – slideWXY Aug 23 '17 at 8:46
  • It's your code that breaks with spaces. All of find, grep, and xargs have command line options to handle awkward filenames cleanly. – roaima Aug 23 '17 at 13:16
0

Command :

ls -ltr|grep "^-"|awk '{print $9}'|xargs rm

Always follow step wise step rule. Its easy to understand and self explanatory. But agree with TOM HUNT, for execution of above command you required permission to do so.

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.