I would like to append the same string for all of the folders name (to the end) in the root folder.
Based on the below question these code may be useful:
for dir in * ; do mv "${dir}" "${dir/_-_ppfsefs/}" ; done
Batch rename folders
However, these code cuts the unwanted parts. And I want to add a string at the end of folder name.
use mv "${dir}" "${dir}_-_ppfsefs", no slashes.
Related
I am a total newbie and can’t pass a problem.
I have a process that transcode movie files to a folder. The files have a file name with two different prefixes.
I want to make all incoming NT_*.mp4 to be moved to one other predefined folder and all ET_*.mp4 to another predefined folder. They predefined with path and name.
Tried a simple if $fname=NT_*.mp4 then mv file to folder1 elif $fname=ET_*.mp4 mv that file to folder2. And that doesn’t work, it just move all files in the folder to folder1, so that is obviously wrong way.
So what do I need to do for checking each file for it prefix and then move them based on the prefix to predefined folders?
Spaces are important. It looks like you used something like
fname="wrong"
if [[ $fname=="right" ]]; then
echo "Equal"
fi
You should add spaces around the equal sign:
fname="wrong"
if [[ $fname == "right" ]]; then
echo "Equal"
fi
I'm trying to organize a bit my music folder. Right now it has many subfolders inside.
Inside every one of those subfolders, there is a subsubfolder with the artist_name which contains music files.
E.g.:
Music_folder/silly_name001/artist_A;
Music_folder/silly_name002/artist_B_discA;
Music_folder/silly_name003/artist_B_discB;
I can list the content of the Music_ folder and get the names of the subfolders with this:
for i in $(ls -d */); do echo ${i%%/}; done
But when I try to move the content of those subsubfolders to the parent folder, I can't achieve my goal. I'm using this code:
for i in $(ls -d */); do mv ${i%%/*} .; done
Actually, it moves the subsubfolders but not the content of them.
Any ideas about how to achieve it?
Thanks in advance
${i%%/*} removes the longest prefix matching /*; see:
$ i=Music_folder/silly_name003/artist_B_discB;
$ echo ${i%%/*}
Music_folder
You're looking for something like this:
set ./*/
echo mv ./*/* .
echo rmdir "$#"
Drop echos if you're happy with the output.
Note that this can't deal with name collisions.
I'm trying append to word "dicom" to the front of many filenames in a set of folders. The folders all begin with "s" (referred to by "s*" in the script below), and each contain many files (specified by "*" below)--I'd like all of these files to be changed using this bash script. I tried to run this:
for file in /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s*/*
do
mv $file dicom${file%%}
done
but got thousands of lines of the following error (once for each file within each folder--this is just an example of one of them):
mv: rename /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/29217684 to dicom/Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/29217684: No such file or directory
Any ideas on how to fix it?
I don't you have a valid path as dicom/Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/, why do you add dicom at the beginning?
maybe you want to append dicom to the end of the $file?
mv "$file" "${file}_dicom"
or something like that.
the following variable expansion ${file%%} is strange because it does nothing:
${parameter%%word} : remove the longest matching suffix pattern.
to move the file into a directory the path should exists, to create the path:
mkdir -p "$(dirname "${newfilename}")"
Maybe what you are trying to do:
for file in /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s*/*
do
mv "$file" "${file%/*}/dicom${file##*/}"
done
A program messed up my directory putting a dot "." on the end of some file and directory names. What is the easiest way to remove them?
I have thought of removing the last character but not all the files/dirs have a dot on the end. Also removing all the dots is a problem, this will make the extension useless.
What I need is a rename to change name.of.the.file.ext. to name.of.the.file.ext and name.of.the.dir. to name.of.the.dir
Thanks!
Go over the files with the dot at the end, rename each if possible (i.e. the target file does not exist).
for file in *. ; do
[[ -e ${file%.} ]] || mv "$file" "${file%.}"
done
echo Not renamed: *.
There might be a rename utility on your machine that will let you do
rename 's/\.$//' *.
Check man rename
I am new to shell scripting.I have a requirement, find out or list .bak(it shld be only .bak, shouldn't select other .bak.tar.gz files while listing) files and then compress them and create .tar.gz files.
Hope these code helps. *.bak will match all .bak files.
tar -czf example.tar.gz *.bak
You probably needs this :
#uncomment 1 of the next 2 lines : the first DOES the cmd, the 2nd only echo it
#thecmd="tar"
thecmd="echo tar"
export thecmd
for bakfile in ./*.bak ; do
[ -f "${bakfile}" ] && ${thecmd} -czf "${bakfile}.tar.gz" "${bakfile}"
done
This also test if the file is a REGULAR file (and not a dir, or a pipe, or a block device) before compressing it into file.tar.gz (note: try to avoid reserved words and variable names in your variable names. There are quite a lot)