Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 '{}' \;`
share|improve this question
    
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 '12 at 20:00
1  
Please add an example of what exactly you want. –  Eric Carvalho Jul 15 '12 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 '12 at 20:25

3 Answers 3

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.

share|improve this answer

I think I understand what you're trying to say.

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 "*.rar" -exec /path/to/script {} rar \;

Replace rar 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 command receives a path and returns the directory where the file is located.
basename command receives a path and returns the file name and optionaly removes a suffix, in this case the extension.

The find command 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 all together the script executes rm -rf /home/user/test/file1.

share|improve this answer
    
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 '12 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 '12 at 21:33
    
That should say REmoves... –  sehesod Jul 15 '12 at 21:44
    
It worked for me. Did you use quotes like the ones in the answer? Added commands explanation. –  Eric Carvalho Jul 15 '12 at 22:57
    
Removed the quotes with parentesis for command substitution. –  Eric Carvalho Jul 15 '12 at 23:00

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
share|improve this answer
    
just fixed to get list of directories, not of files. –  richard Jul 15 '12 at 22:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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