2

I have many files that i want to move into folders, their names are something like this dir1/dir2/dir3/name_of_file.ex and then maybe another is called /dir4/name_of_file2.xe

Is there anyway of moving those files into the directory dir1/dir2/dir3 and change the name of it to only name_of_file.ex automatically?

I really don't know how i can explain it easier.. but i have almost a 1000 files with names like TSData\Res\Catalog\Scripts\deckfoundations.txt and TSBin\Sims2.exe and what i want to do with those files is to move into the folder TSData\Res\Catalog\Scripts\ and then rename the file to only deckfoundations.txt. don't know if you know PHP but there you have a explode function where you explode the name of the file with a character

$ar = end(explode("/", $filename));
print_r($ar);

would output something like this

Array
(
   [0] => TSData\Res\Catalog\Scripts
   [1] => deckfoundations.txt
)

Then with that i could use the first row to create the folder if not existing and then rename the full $filename to the second row of $ar

2

2 Answers 2

2

You could try a file renaming tool such as those listed in the link provided by tachyons in the comments.

If that fails you may need to script something.

As you say in the question, you know PHP, why not write a solution yourself in PHP. What you suggest seems a reasonable approach.

1

I have a script that I use to find, rename, and move files all in one go. You can modify it to suit your purpose if you want; it is currently set to randomly rename files, but you could set a pattern to rename them by.

By the way, the script moves all the files to the current working directory (the one it is executed in), but again that could be modified. You will obviously have to change the ~/Downloads location and substitute the .jpg extensions for what you need.

I am not quite sure that this is exactly what you want, but it may be useful as a template for you and anyone else. Save it in your favourite text editor and make it executable.

NOTE: if you modify the script, but keep the random renaming feature, be aware that occasionally there can be collisions (i.e. the same numbers are generated and the new file is overridden), as there are a limited number of new file names that even two instances of $RANDOM can generate. It is unlikely to happen as the files are being labelled with two sets of random numbers, but it might be better to have a different numbering system if your files are very important or critical in any way.

#!/bin/bash
# a script to recursively find all jpgs in a specified directory, rename with a random label and transfer to pwd

find ~/Downloads -type f -iname '*.jpg' -print0 | 
while IFS= read -r -d '' f; 
do mv -- "$f" "$RANDOM-$RANDOM.jpg"; 

done

You must log in to answer this question.

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