简体   繁体   中英

centos Shell script to mv dynamic file names

I have the following file structure:

/uploads/<blog_id>/10/155

For every blog_id there is a folder named 10 and inside that folder is another folder 155 . folder 155 has a bunch of files in it.

I am simply trying to move all files from /10/155 to simply /10 for all blog_id s.

Basically, I just want to eliminate the 155 folder, moving its contents down into 10 . I don't care if 155 stays there or is deleted.

I have tried several versions of this script:

for file in /var/www/html/uploads/*/10/155; do mv $file ${file%/*}/10; done

Ultimately, I was able to rename the 155 folder to 10 like: */10/10 . I moved 155 down 2 levels (next to 10 ) like: */155 .

Any help?

Just loop over the blog_id directories. Then for each of those you can do what you need:

for blog_dir in /uploads/*; do
    mv "$blog_dir"/10/155/* "$blog_dir"/10/
    rmdir "$blog_dir"/10/155
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM