Renaming bulk directories and subfiles


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming bulk directories and subfiles
# 1  
Old 10-03-2013
Renaming bulk directories and subfiles

Hi,

I have a directory with 100 subdirectories and each of these subdirectories has 1 file. Now I have to rename all these.

The structure is "files directory has 100 SRR191639-SRR191718 subfolders and in each there is a file with the same name a subdirectory followed by .sra extension (SRR191639.sra)

example files->SRR191639->SRR191639.sra

Like this I have 100 files

I have a another file which has the sample names corresponding to these SRR numbers. Now I want to rename the subdirectories and the file in those directories with the sample names.

Sample name file:
Code:
ABC_123 SRR191639
XYZ_IRT SRR191640
ABC_1LK SRR191641

output

The subdirectories and the child files should be renamed to

Code:
ABC_123->ABC_123.sra
XYZ_IRT->XYZ_IRT.sra
ABC_1LK->ABC_1LK.sra

Thanks,
# 2  
Old 10-03-2013
If i understood you properly you actualy just want to rename the folders.
They are named like ABC_123 and should be named ABC_123.sra.

Code:
cd /path/to/folders
for each in $(ls);do mv $each $each.sra;done

Hope this helps

EDIT:
Code:
cd /path/to/folders
for each in $(ls);do
    for eachfile in $(cd $each;ls);do mv $each/$eachfile $each/$eachfile.sra;done # Renames the files in folder $each
    mv $each $each.sra # Renames the folder
done


Last edited by sea; 10-03-2013 at 08:42 PM..
# 3  
Old 10-03-2013
Something like:
Code:
cd files
for i in SRR*
do
   newname=$(awk -vsrch=$i '$0 ~ srch {print $1}' samples.txt)
   if [ "x"$newname != "x" ]
   then
      echo mv $i/$i.sra $i/$newname.sra
      echo mv $i $newname
   fi
done

Remove the echo when you're sure it works.
# 4  
Old 10-03-2013
Thanks for the reply.

Nope I want to change the directory name and also the filename in the directory to the names I have in sample_info_file.txt

My sample_info_file.txt has two columns one with the directories name and other with its corresponding sample name. So I want the program to read my sample_info file and based on its corresponding directory name change both the directory and the its child file name to the sample name.

Hope I am clear
# 5  
Old 10-03-2013
Could you share a few example lines of sample_info_file.txt?
# 6  
Old 10-04-2013
Thanks Carlo. It worked..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming directories stops resumption of write process

so lets say I have a process in cron that writes to a particular directory /var/tmp/EXAMPLEA so, from time to time, say every couple of months, an upgrade is made. and here's how the upgrade works: 1. move the existing directory /var/tmp/EXAMPLEA somewhere else and name it differently...i.e.... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. 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

3. Shell Programming and Scripting

Renaming files in bulk.

Hi, I have a bunch of files which are named something like: Company Name~1234~X1234~X1-123.pdf I need to get them renamed something like: Company Name~1234(X1234)X1-123.pdf Once I have the X1234 inside () I have a piece of software which can use the X1234 bit. I will be receiving... (7 Replies)
Discussion started by: jcborland
7 Replies

4. Shell Programming and Scripting

bulk renaming of files in sftp using script

Hi, Am using sftp (dsa method) to transfer 20 files from one server(sftp) to another (local). After the transfer is complete the files in the sftp server has to be renamed from .txt extension to .done extension ( aa.txt to aa.done, bb.txt to bb.done and likewise...). I tried rename command... (4 Replies)
Discussion started by: Sindhuap
4 Replies

5. Shell Programming and Scripting

renaming directories with shell script

Hi All after looking around the website and various other resources I become stuck. I'm trying to rename directories from Firstname Initial Lastname to lastname,_firstname_initial so far ive got for f in {./} do rename -n 'y/A-Z/a-z/' * rename -n 's/\ /_/g' * ... (2 Replies)
Discussion started by: harlequin
2 Replies

6. 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

7. UNIX for Dummies Questions & Answers

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... (2 Replies)
Discussion started by: robotsbite
2 Replies

8. 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

9. Shell Programming and Scripting

Need help renaming bulk file extentions

Hello, I am trying to rename bulk files however i dont think the rename/mv command is giong to help me here. here is a quick snapshot of the files I need to rename: 75008040 -rw-r----- 1 root root 8716 May 8 05:00 10.9.144.2 75008041 -rw-r----- 1 root root 11700 May 8 05:00 10.9.160.2... (10 Replies)
Discussion started by: jallan
10 Replies

10. Shell Programming and Scripting

outcomment bulk

Hello chiefs, So i wanna outcomment several lines, but i dont want to start each line with #-sign. Once i saw it in use, but dont remember the syntax. It should work with sh or ksh. regards congo (3 Replies)
Discussion started by: congo
3 Replies
Login or Register to Ask a Question