Help with moving files with same names from subdirectories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with moving files with same names from subdirectories?
# 1  
Old 05-10-2011
Help with moving files with same names from subdirectories?

I was wondering if someone could help me with this:

I have multiple directories and subdirectories with files in them. I need to move all files from all directories and subdirectories into one root directory. However, when i do
Code:
find /home/user -mindepth 1 -iname "*" -type f -exec mv {} . \;

The command takes only 1 copy of files with the same name and ignores all other files. Ideally I would like it to rename files with same file names to something else. Like appending a number etc.

Example:
Code:
/home/user/test/setup.log
/home/user/best/setup.log

when moved to a folder should be setup.log1 setup.log2 and so on.

Any help is much appreciated

Last edited by Franklin52; 05-10-2011 at 03:26 AM.. Reason: Please use code tags
# 2  
Old 05-10-2011
After finding the files, write a small if condition to check if the file is already present and rename it by appending 1 or 2 and then copyig the others...
# 3  
Old 05-10-2011
After hours of trying i managed to find a very crude way of doing this. Posting it so someone else might find it useful, or correct my script.

Code:
 find /media/perl/bin/Malco -mindepth 2 -iname "*" -type f -exec md5sum {} \; | sed -r "s/(^.*)(\s+)(.*)/rename \"\3\" \1/" > malco_rename.sh

I then execute malco_rename.sh. Don't know how to do it in the same line.

Cheers
# 4  
Old 05-10-2011
Code:
# find /somedir/ -type f
/somedir/testx/testx1/98
/somedir/testx/testx1/5
/somedir/testx/testx1/1
/somedir/testx/testx1/2
/somedir/testx/testx1/8
/somedir/testx/testx1/3
/somedir/testx/testx1/7
/somedir/testx/5
/somedir/testx/4
/somedir/testx/1
/somedir/testx/2
/somedir/testx/3
/somedir/testx/testx2/4
/somedir/testx/testx2/1
/somedir/testx/testx2/2
/somedir/testx/testx2/3

Code:
# find /somedir -type f|while read -r a ; do if [[ ! $(ls -1|grep "^${a#${a%/*}/}_*") ]] ; then \
cp -f $a .; else cp -f $a ${a#${a%/*}/}_$(echo $(ls -1|grep "^${a#${a%/*}/}_*"|wc -l)+1|bc) ; fi ; done ; ll
-rw-r--r-- 1 root root 0 Jan 21 17:04 1
-rw-r--r-- 1 root root 0 Jan 21 17:04 1_2
-rw-r--r-- 1 root root 0 Jan 21 17:04 1_3
-rw-r--r-- 1 root root 0 Jan 21 17:04 2
-rw-r--r-- 1 root root 0 Jan 21 17:04 2_2
-rw-r--r-- 1 root root 0 Jan 21 17:04 2_3
-rw-r--r-- 1 root root 0 Jan 21 17:04 3
-rw-r--r-- 1 root root 0 Jan 21 17:04 3_2
-rw-r--r-- 1 root root 0 Jan 21 17:04 3_3
-rw-r--r-- 1 root root 0 Jan 21 17:04 4
-rw-r--r-- 1 root root 0 Jan 21 17:04 4_2
-rw-r--r-- 1 root root 0 Jan 21 17:04 5
-rw-r--r-- 1 root root 0 Jan 21 17:04 5_2
-rw-r--r-- 1 root root 0 Jan 21 17:04 7
-rw-r--r-- 1 root root 0 Jan 21 17:04 8
-rw-r--r-- 1 root root 0 Jan 21 17:04 98

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. UNIX for Dummies Questions & Answers

How to list the names of the files from all the subdirectories?

Hi, I'm currently trying to print the names of all the .txt files in the subdirectories that contain the string I'm searching. I tried with this code, but it seems that it searches for the names that matches the string instead of searching for the string in the individual files and printing the... (2 Replies)
Discussion started by: nuclearpenguin
2 Replies

5. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

6. UNIX for Dummies Questions & Answers

Moving files with specific names

Hi, I have a list of names (in a text file) like this: SRR1234 SRR5678 SRR4321 SRR8876 I'd like to have a unix code to find all the files which have any of above strings in their name and move them to a specific directory. I have my files distributed in many subdirectories so it has to... (3 Replies)
Discussion started by: a_bahreini
3 Replies

7. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

8. Shell Programming and Scripting

Moving files to their respective subdirectories

Hello, I have files like this img.txt ./Rearrange_zoca/fitted_data/WX226_05b_Ncere.jpg ./Rearrange_zoca/fitted_data/w322_03B_60_70.jpg ./Rearrange_zoca/fitted_data/wrx226_12A_50_60.jpg ./Rearrange_zoca/fitted_data/wx124_01A_50_60.jpg ./Rearrange_zoca/fitted_data/wx124_02A_50_60.jpg... (3 Replies)
Discussion started by: avatar_007
3 Replies

9. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

10. UNIX for Dummies Questions & Answers

Moving files and changing names

i have many files with extention filename.ASN_ERROR~ at a path. I want to move these files to another path and change extension to .ASN There are more then 80,000 files so i cant use manual commands muneebr (5 Replies)
Discussion started by: muneebr
5 Replies
Login or Register to Ask a Question