Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I need to make some changes to a series of directories and sub directories and I need help !
I have a series of directories : "folder a", "folder b", "folder c", ....
and in these directories there are sub directories "folder 1", "folder 2" ,....
I need to rename "folder 1" in "folder a" to "folder a -folder 1" and move it to main directory. so there wont be any sub directories .
the directory tree is :
Code:
main directory
-folder a
-- folder 1
-- folder 2
....
-folder b
-- folder 1
-- folder 2
....
first, fix the file names so they don't have spaces in them---Windows somehow tolerates this, but in a real OS like Unix or Linux, spaces in file names = trouble.
Start by defining the individual commands---eg, if you are in "folder_a", you could issue:
Code:
mv folder_1 ../folder_a-folder_1
but, we need it to be generic, so use pwd to get the current folder:
Code:
mv folder_1 ../$(pwd)-folder_1
Once you have the right commands, then put it inside a loop
Thanks for your help !
I know how to manually use mv .
the problem is that in the main folder there is over 500 folders where in each folder is some sub-folders with deferent names !
and do that manually takes lots of time .
PS;
If you know that directories and sub-directories contain only files (or only directories), then looping thru them is easier. Otherwise, the code will need to test each item to see what it is.
The awk filters out just the ones with two levels, and for each of them makes a mv command to do the rename. This is piped to a shell to actually do the mv commands. The "backslash 0 4 7" is an escape for a single quote, which you will need to enclose your path names in the mv commands because you have blanks in the file names.
For testing, just leave off the bash part and check the commands visually to ensure they do what you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.