Renaming files after their directory name in multiple sub directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming files after their directory name in multiple sub directories
# 1  
Old 07-15-2009
Question Renaming files after their directory name in multiple sub directories

So I am not sure if this should go in the shell forum or in the beginners. It is my first time posting on these forums.

I have a directory, main_dir lets say, with multiple sub directories (one_dir through onehundred_dir for example) and in each sub directory there is a test.txt. How would one rename all the text.txt files after thier respective subdir name.
so from:

main_dir/one_dir/test.text
main_dir/two_dir/test.text
...
...

to:
main_dir/one_dir/one_dir.text
main_dir/two_dir/two_dir.text
...
...
Any assistance would be really helpful. And if you could point me to a resource to learn these kind of things on my own without just reading all the man pages I would appreciate that as well.
Thanks,
robotsbite
# 2  
Old 07-15-2009
hmm, learning UNIX without reading the man pages is nigh impossible. Yes, they are tough to get used to, but i assure you that once you have acquired some UNIX skills and have grown used to using them you will ask why not everybody could have the help texts organized the same way. Believe me: try them for some time and you will grow to love them.

To your problem at hand: this is a typical problem for using the "find" command. "find" can be used to find files, as the name suggests, but in fact it is a sort-of command-line file manager, fully programmable and extensible.

Find a short explanation of how "find" works here: find command explained.

Since you said you want to learn I'll leave it at that for now. Try to create a solution based on that, try to understand the man pages of "find" and if you still have questions feel free to ask.

I hope this helps.

bakunin
# 3  
Old 07-16-2009
Thanks for the assistance. I was looking into find already and if I simply wanted to find all of the .txt files and name them something that I preset I could do that. I did not want to say that I thought I should use find in case there was another command out there that I did not know about that is better.

Where I am stuck though is the naming of the file after its directory name. I just do not see and example of that anywhere. My only thought would be to use find twice maybe?
find <the sub_dir> find <the .txt> mv <the .txt> <<sub_dir>.txt>
Any thoughts?
I'm still trying to find the right way to make it work.

---------- Post updated at 09:10 AM ---------- Previous update was at 08:23 AM ----------

Is there a way to specify which -exec variable you re dealing with if you have more than one?
I have been trying:
find . -type d -exec find {} \; -type f -name "*.txt" -exec mv {} (the file) {}.txt(the dir name that the fiel being renamed is in)
no luck so far
In testing I am just trying to echo both brackets:
find . -type d -exec find {} \; -type f -name "*.txt" -exec echo "one {} two {}" \;
But all I get is a print of all directories and all .txt files and it does not display the "one" or "two"

---------- Post updated at 09:54 AM ---------- Previous update was at 09:10 AM ----------

For the record I talked with a friend and came up with this.
For a file system that looks like this:

a
`-- b
|-- c
| |
| `-- somename.txt
`-- d
|
`-- somename.txt
> cd a/b/
> for x in *;do mv $x/*-.txt $x/${x}.txt;done

I hope this helps anyone else in the future.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy particular files from a multiple directories and paste in a new directory?

Dear all I have a multiple directories, say for example org1, org2, org3 ..... org100 and each directory having a file namely dnaG.fasta. I need to copy all the dnaG.fasta file from each directory and paste in another directory fastconcatg. Therefore, my script has to copy dnaG.fasta file from... (5 Replies)
Discussion started by: dineshkumarsrk
5 Replies

2. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

3. Shell Programming and Scripting

Renaming files in multiple directories

Hi I have the following file structure and I want to rename all the abc.jar files to abc_backup.jar rock@server:~/rakesh> ls -R .: test1 test2 test3 ./test1: abc.jar ./test2: abc.jar ./test3: abc.jar (2 Replies)
Discussion started by: rakeshkumar
2 Replies

4. Shell Programming and Scripting

Moving and renaming multiple files in a directory

Hi. I am trying to automate the movement and renaming of a number of files in a directory. I am using the 'mv' command as I do not have access to 'rename'. I have the following scripted FILES=$(ls /transfer/move/sys/mail/20130123/) if ; then for i in ${FILES} ; do mv... (4 Replies)
Discussion started by: jimbojames
4 Replies

5. Shell Programming and Scripting

Renaming multiple files in a directory

Hello, I would like to rename all available files in a directory from Filename to Filename_Normal. I tried to use below script but it is giving some error: #!/bin/sh for i in `ls` do echo Changing $i mv $i $i_Normal done Error received: Usage: mv src target or: mv ... (10 Replies)
Discussion started by: manishdivs
10 Replies

6. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

7. Shell Programming and Scripting

Copy files from multiple directories into one directory without overwriting them

I have several directories and all those directories have .dat files in them. I want to copy all those .dat files to one directory say "collected_directory" The problem is I don't want to overwrite files. So, if two file names match, I don't want the old file to be overwritten with a new one. ... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

9. UNIX for Dummies Questions & Answers

renaming files in the directory

suppose i have a few file like "a b c.txt" , "hello world.c" etc...(files that have space between them in their filenames). how do i change their filenames to "a_b_c.txt" and "hello_world.c" respectively? heres what i have tried...but failed $ find -name "*" -exec mv "{}" "`echo {} | tr "... (4 Replies)
Discussion started by: c_d
4 Replies

10. UNIX for Dummies Questions & Answers

Batch Renaming: Change files' extensions in many sub-directories

Hi all - I'm trying to rename a large number of files all at once and need some help figuring out the command line syntax to do it. I've already done quite a bit of research with the rename and mv commands, but so far haven't found a solution that seems to work for me. So: The files exist... (10 Replies)
Discussion started by: dave920
10 Replies
Login or Register to Ask a Question