📜 ⬆️ ⬇️

We continue to delete. [Re: Working with “bad” files on the Linux command line]

As you know, the world of GNU / Linux is diverse. For the same problem there are many solutions.

Sometimes one linuksoid solves the problem, and the second looks at this process and has an irrepressible desire to intervene, to do it their own way. In such conditions, this record was born.

The topic in which the topic of deleting files with incorrect names was touched is here . Next will be given other methods for solving the same problem.
')
It is assumed that it is possible to use only the command line without file managers. By “correct” file we mean a file with a name like number.txt, by “incorrect” - all others.

Interactive deletion


The method is extremely convenient if there is a small number of files in the directory (or you can select a certain subset of them with a wildcard), some of which need to be deleted.
The rm command has the -i option, which prompts the user for confirmation to delete the file.

$ ls -lAh -rw-r--r-- 1 user user 0 May 15 01:47 ? -rw-r--r-- 1 user user 0 May 15 01:47 ? -rw-r--r-- 1 user user 0 May 15 01:47 ? -rw-r--r-- 1 user user 0 May 15 01:48 1.txt -rw-r--r-- 1 user user 0 May 15 01:48 ?.txt $ rm -i * rm: remove regular empty file `\n'? y rm: remove regular empty file `\v'? y rm: remove regular empty file `\f'? y rm: remove regular empty file `1.txt'? n rm: remove regular empty file `\r.txt'? y $ ls -lAh -rw-r--r-- 1 user user 0 May 15 01:48 1.txt 

Quick and easy.

Denying wildcard


Let's complicate the task by creating 100 files with the correct name and 100 incorrect ones:
 $ touch {1..100}$'\x0a'.txt $ touch {1..100}.txt $ ls -A ? 15?.txt 22?.txt 2?.txt 37?.txt 44?.txt 51?.txt 59?.txt 66?.txt 73?.txt 80?.txt 88?.txt 95?.txt ? 16.txt 23.txt 30.txt 38.txt 45.txt 52.txt 5.txt 67.txt 74.txt 81.txt 89.txt 96.txt ? 16?.txt 23?.txt 30?.txt 38?.txt 45?.txt 52?.txt 5?.txt 67?.txt 74?.txt 81?.txt 89?.txt 96?.txt 100.txt 17.txt 24.txt 31.txt 39.txt 46.txt 53.txt 60.txt 68.txt 75.txt 82.txt 8.txt 97.txt 100?.txt 17?.txt 24?.txt 31?.txt 39?.txt 46?.txt 53?.txt 60?.txt 68?.txt 75?.txt 82?.txt 8?.txt 97?.txt 10.txt 18.txt 25.txt 32.txt 3.txt 47.txt 54.txt 61.txt 69.txt 76.txt 83.txt 90.txt 98.txt 10?.txt 18?.txt 25?.txt 32?.txt 3?.txt 47?.txt 54?.txt 61?.txt 69?.txt 76?.txt 83?.txt 90?.txt 98?.txt 11.txt 19.txt 26.txt 33.txt 40.txt 48.txt 55.txt 62.txt 6.txt 77.txt 84.txt 91.txt 99.txt 11?.txt 19?.txt 26?.txt 33?.txt 40?.txt 48?.txt 55?.txt 62?.txt 6?.txt 77?.txt 84?.txt 91?.txt 99?.txt 12.txt 1.txt 27.txt 34.txt 41.txt 49.txt 56.txt 63.txt 70.txt 78.txt 85.txt 92.txt 9.txt 12?.txt 1?.txt 27?.txt 34?.txt 41?.txt 49?.txt 56?.txt 63?.txt 70?.txt 78?.txt 85?.txt 92?.txt 9?.txt 13.txt 20.txt 28.txt 35.txt 42.txt 4.txt 57.txt 64.txt 71.txt 79.txt 86.txt 93.txt ?.txt 13?.txt 20?.txt 28?.txt 35?.txt 42?.txt 4?.txt 57?.txt 64?.txt 71?.txt 79?.txt 86?.txt 93?.txt 14.txt 21.txt 29.txt 36.txt 43.txt 50.txt 58.txt 65.txt 72.txt 7.txt 87.txt 94.txt 14?.txt 21?.txt 29?.txt 36?.txt 43?.txt 50?.txt 58?.txt 65?.txt 72?.txt 7?.txt 87?.txt 94?.txt 15.txt 22.txt 2.txt 37.txt 44.txt 51.txt 59.txt 66.txt 73.txt 80.txt 88.txt 95.txt 

The case is a bit contrived, but allows you to show a new method. Assuming that we need files of the form number.txt, using the deny wildcard, we choose what to delete:
 $ ls *[!0-9].txt 100?.txt 17?.txt 24?.txt 31?.txt 39?.txt 46?.txt 53?.txt 60?.txt 68?.txt 75?.txt 82?.txt 8?.txt 97?.txt 10?.txt 18?.txt 25?.txt 32?.txt 3?.txt 47?.txt 54?.txt 61?.txt 69?.txt 76?.txt 83?.txt 90?.txt 98?.txt 11?.txt 19?.txt 26?.txt 33?.txt 40?.txt 48?.txt 55?.txt 62?.txt 6?.txt 77?.txt 84?.txt 91?.txt 99?.txt 12?.txt 1?.txt 27?.txt 34?.txt 41?.txt 49?.txt 56?.txt 63?.txt 70?.txt 78?.txt 85?.txt 92?.txt 9?.txt 13?.txt 20?.txt 28?.txt 35?.txt 42?.txt 4?.txt 57?.txt 64?.txt 71?.txt 79?.txt 86?.txt 93?.txt ?.txt 14?.txt 21?.txt 29?.txt 36?.txt 43?.txt 50?.txt 58?.txt 65?.txt 72?.txt 7?.txt 87?.txt 94?.txt 15?.txt 22?.txt 2?.txt 37?.txt 44?.txt 51?.txt 59?.txt 66?.txt 73?.txt 80?.txt 88?.txt 95?.txt 16?.txt 23?.txt 30?.txt 38?.txt 45?.txt 52?.txt 5?.txt 67?.txt 74?.txt 81?.txt 89?.txt 96?.txt 

Making sure that everything is in order, we delete these files.
 $ rm *[!0-9].txt 

find


The attentive reader probably noticed that in the previous example I missed 3 wrong files. I'll fix it.
In the directory there are 100 files of the type number.txt and 3 files with a kind of strange name.

One of the methods for isolating unnecessary files for us is find in conjunction with xargs (the good old custom of bearded administrators * nix to link utilities through a pipe)

 $ find . -type f -not -name '*txt' ./? ./? ./? $ find . -type f -not -name '*.txt' | xargs rm rm: cannot remove `./': Is a directory 

Hmm, some of the files turned out to be annoying.

 % ls -A ? 15.txt 21.txt 28.txt 34.txt 40.txt 47.txt 53.txt 5.txt 66.txt 72.txt 79.txt 85.txt 91.txt 98.txt 100.txt 16.txt 22.txt 29.txt 35.txt 41.txt 48.txt 54.txt 60.txt 67.txt 73.txt 7.txt 86.txt 92.txt 99.txt 10.txt 17.txt 23.txt 2.txt 36.txt 42.txt 49.txt 55.txt 61.txt 68.txt 74.txt 80.txt 87.txt 93.txt 9.txt 11.txt 18.txt 24.txt 30.txt 37.txt 43.txt 4.txt 56.txt 62.txt 69.txt 75.txt 81.txt 88.txt 94.txt 12.txt 19.txt 25.txt 31.txt 38.txt 44.txt 50.txt 57.txt 63.txt 6.txt 76.txt 82.txt 89.txt 95.txt 13.txt 1.txt 26.txt 32.txt 39.txt 45.txt 51.txt 58.txt 64.txt 70.txt 77.txt 83.txt 8.txt 96.txt 14.txt 20.txt 27.txt 33.txt 3.txt 46.txt 52.txt 59.txt 65.txt 71.txt 78.txt 84.txt 90.txt 97.txt 

As you can see, he is the only one.

extglob


Bash has many options. We are interested in extglob - the use of advanced pattern matching capabilities (pattern matching).
 $ shopt extglob extglob off $ shopt -s extglob $ shopt extglob extglob on 

After the option is enabled, the user becomes (in particular) able to use a “denying wildcard”, acting entirely on the list of samples:
 $ ls !(*.txt) ? $ rm !(*.txt) $ ls * | wc 100 100 692 

There are 100 correct files left, success!
Disable extglob if it is no longer needed:
 $ shopt -u extglob 

Thanks for attention.

Used materials:
man rm
man bash
man find

Source: https://habr.com/ru/post/179837/


All Articles