2

I am currently working on recovering some logfiles from random backups that I made. I have 48 files in 70+ folders.

How can I move these 48 files into one folder, without overwriting files with the same name? Most of these files have the same name, but different size, I checked with fdupes.

2
  • What do you want to do with files that have the same name ?
    – NorTicUs
    Oct 20, 2012 at 10:55
  • I want to keep them, renamed. (I will sort the files by the date it was created, so the name doesn't matter that much)
    – ubunchu
    Oct 20, 2012 at 15:34

2 Answers 2

0

Type this command in your terminal (Ctrl+Alt+T to open it) :

find $LOG_DIR -type f | xargs -I {} sh -c 'base=$(basename "$1"); name=${base%.*}; ext=${base##*.}; mv "$1" "$OUTPUT_DIR/$name`date +%N`.$ext"' -- {}

Basically, this will find all your files in $LOG_DIR (that you have to replace with the emplacement of the parent of your 70 folders), extract the name without path (basename), separate the name and the extension and then move them to $OUTPUT_PATH (replace by where you want them to go) and append the number of nanoseconds at this time (data +%N) to the name, so no overwrite.

PS: I've got a little help from there : StackOverflow

9
  • Hi, thanks for the answer! I have tried the command, though I might have misunderstood the $LOG_DIR thing and the $OUTPUT_PATH. I have tried this way: ""find $USER Terminalling/a1command/extract_these_files/ ...."" and also: ""find Terminalling/a1command/extract_these_files/..."". Either way i got: ""mv: cannot stat ‘thisisalogfile.log’: No such file or directory"" or ""mv: target ‘Terminalling/a1command/excerpt/757786046thisisalogfile.log’ is not a directory"". Here is what i have done: pastebin.com/mZGvUp8t
    – ubunchu
    Oct 24, 2012 at 12:41
  • My bad, this line only work if you're in the same folder than the files you want to move. I'll edit my answer. And what's Terminalling ? A folder ?
    – NorTicUs
    Oct 24, 2012 at 12:45
  • Yes, its a folder. I have tried the command from the parent directory, but same result. (and from within the folder where the file is, it only renamed it) I'm guessing the command needs recursive search in all existing subdirectories, and moving the found files to another folder.
    – ubunchu
    Oct 24, 2012 at 12:55
  • Try this new line. I tested it with logs in a test folder, launching it from my home.
    – NorTicUs
    Oct 24, 2012 at 13:06
  • mv: cannot create regular file ‘/thisisalogfile412781517.log’: Permission denied. ---tried the command as it is, from the parent folder
    – ubunchu
    Oct 24, 2012 at 13:09
0

First use find as indicated above to locate the files you want to move:

find /dir/with/scattered/logs -type f

This will just print the list of files. If you're happy with it, simply use cp with the backup option, this will "make a backup of each existing destination file". Since you said names don't matter that much that should be OK. Backed-up files will have a "~" attached to the name. See man cp for some better control over this functionality.

find /dir/with/scattered/logs -type f -exec cp -b {} /destination/directory \;

This will copy files one by one, which is less efficient than using xargs, but for the amount of files you mention it shouldn't be an issue.

You must log in to answer this question.

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