2

I've been looking for a code snippet to place in a .sh that removes a directory named after a file.

What I mean by this is I would like to move to specify a directory and then search it for a list of files of type ".XXX" and then remove any subdirectories (with contents) with the same name.

Pseudo code of what i want to do:

find names all files of type .XXX within a directory
find and remove all subdirectories with names that equal the file names found

How would one best go about this? I have been looking at the find function, which I will be using before this function to unrar archives:

`find . -name "*.rar" -exec unrar x '{}' \;`
3
  • Please consult your 'man' page for the command "rm" Open a terminal and type "man rm" this is the basic CLI for removing files and directories.
    – Ringtail
    Jul 15, 2012 at 20:00
  • 1
    Please add an example of what exactly you want. Jul 15, 2012 at 20:02
  • @blueXrider Is it? Just using rm or rmdir of course I can remove directories. The tricky part, I find, is searching the directory for filenames and then using those to remove directories. Any thoughts on that?
    – sehesod
    Jul 15, 2012 at 20:25

3 Answers 3

0

What's not very clear here is that you can't have a file and a directory with the same name. Also, since you mention uncompressing rar files, one could think that you want to remove the directory named after the rar file. This is strange because, why would you want to delete it after you uncompressed it?

Based on my poor understanding of the problem (which may improve if you clarify or, better yet, provide an example), this may work:

for i in *rar; do
    (cd uncompressed && rm -rf $i)
done

so if you have something like

./file1.rar
./file2.rar
./file3.rar
./uncompressed/file1.rar/...
./uncompressed/file2.rar/...
./uncompressed/file3.rar/...

the mini-script I posted should delete all the file*.rar/ subdirectories under uncompressed.

0

If you wish to find all directories d, there d contains a file d.xyz. Then this will find the directories for you.

find . -type d -print0 |
xargs -0 --replace={} --max-args=1 bash -c 'find {} -maxdepth 1 -name $(basename {}).xyz -print0' |
xargs -0 --replace={}  bash -c 'echo $(dirname {})'

explanation:

  1. find all files of type directory that are descendants of current working directory
  2. then for each directory d (and strictly one at a time), that has direct children who's name is d.xyz
  3. print the directory name
1
  • just fixed to get list of directories, not of files. Jul 15, 2012 at 22:26
0

Create a shell script like this:

#!/bin/bash
rm -rf $(dirname $1)/$(basename $1 .$2)

And use the following to find files and remove the directories:

find . -name "*.XXX" -exec /path/to/script {} XXX \;

Replace XXX with the extension you want to search.

What the script does:

  • First parameter is the path of a file.
  • Second parameter is the extension you're searching.
  • dirname receives a path and returns the directory where the file is located.
  • basename receives a path and returns the file name and optionally removes a suffix, in this case the extension.

find passes to the script the path to the compressed file searched and the extension searched, e.g. /path/to/script /home/user/test/file1.rar rar.

dirname $1 expands to dirname /home/user/test/file1.rar which returns /home/user/test, basename $1 expands to basename /home/user/test/file1.rar rar which returns file1. Putting it all together the script executes rm -rf /home/user/test/file1.

5
  • Ah! This looks something like what I want to do. Could you by any chance explain the code in the shell script more thoroughly? I'm very new to Ubuntu and *nix.
    – sehesod
    Jul 15, 2012 at 21:09
  • Your script unfortunately moves the files found, rather than the directories named like them. Any thoughts on why? I do not fully understand your code. A big thanks for your effort to answer my question!
    – sehesod
    Jul 15, 2012 at 21:33
  • That should say REmoves...
    – sehesod
    Jul 15, 2012 at 21:44
  • It worked for me. Did you use quotes like the ones in the answer? Added commands explanation. Jul 15, 2012 at 22:57
  • Removed the quotes with parentesis for command substitution. Jul 15, 2012 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .