Environment: CentOS, shell
I have a few files under a directory with the names like
B?orn.txt
F?ord.xml
etc
How to find/rename all files under the directory containing ? and delete this ? in the filenames to make all them like:
Born.txt
Ford.xml
etc
If you have the rename utillity installed, just use that command:
$ rename -n 's/\?//' dir/*
dir/f?ile renamed as dir/file
dir/f?ile2 renamed as dir/file2
dir/f?ile3 renamed as dir/file3
The -n flag is to check what the utillity would do, remove it to do the rename.
Related
I need to rename files in multiple directories to the name of the parent directory and move them up one directory and the delete the empty directories.
Sample structure:
/export
exp_20210101
3747-46473-328383-5555
exp_20210102
4533-45323-354345-5366
Desired result:
/export
exp_20210101
exp_20210102
3747-46473-328383-5555 renamed to exp_20210101
No extensions (Linux)
I prefere to do it with a bash script.
Already tried several samples (similar questions), but they're not working for my case.
I need to rename files in multiple directories to the name of the parent directory and move them up one directory and the delete the empty directories.
I don't think that's possible, you can't have a regular file and a directory with the same name in the same directory.
You should move each file to the same level as its parent without changing its name instead (assuming a file with the same name may not exist there). Then you can remove the parent and rename the file to its name.
for f in /export/*/*; do
echo mv "$f" "${f%/*/*}"
echo rmdir "${f%/*}"
echo mv "${f%/*/*}/${f##*/}" "${f%/*}"
done
Drop echos if the output looks good.
I am trying to use rename but I have several subdirs. The expression I am trying to use is this:
rename 's/ZAUQ-F24MS-SC12-F01-5C\/R44.wav/wav\/2012.wav/' *.wav
I want to rename the file ZAUQ-F24MS-SC12-F01-5C/R44.wav to wav/2012.wav and all files are *.wav files.
I am able to do this from inside the directory but I have multiple directories and a mapped to and from list. Can rename do this or should I be using something else?
Apparently you are moving a file from one directory (ZAUQ-F24MS-SC12-F01-5C) to another (wav). Why not mv then:
mv ZAUQ-F24MS-SC12-F01-5C/R44.wav wav/2012.wav
sorry i am new to shell scripting
I have few folders in PWD. i need to copy one file to all the folders in PWD. FIle is also stored in present working directory . How can i do that?
maybe something like:
for d in */; do cp -v ./file "$d/"; done
Suppose I have a folder named my_folder_old in /path/to/folder, how can I create a duplicate named my_folder_new in the same directory?
EDIT
Moreover if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
Tutorial copy files, folder link: link
Manual cp command : Link
cp -frp /path/to/folder/my_folder_old -T /path/to/folder/my_folder_new
-f, --force
if an existing destination file cannot be opened, remove it
and try again (this option is ignored when the -n option is
also used)
-p same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
copy directories recursively
-T, --no-target-directory
treat DEST as a normal file
Though if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
The reason why is this happening because, my_folder_new already created. Doing same cp command it will see as new path, /path/to/folder/my_folder_new/
I was dealing with this same issue, was going crazy ahaha, I tried cp -frp but did not work, so, before of going to do cp just remove the existing folder using rm, see below more info about this:
Remove Directory Linux
If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:
rm -rf dir1
i am having tar file "test.tar.gz'.now i want to get the name "test' after untar the "test.tar.gz'. In shell script ,how to get this.
To strip an extension from a file name, use basename:
basename test.tar.gz .tar.gz
prints test
But that is just the base name of the archive. It's not always the name of any file or directory which the tar creates. The tar archive can contain any file names. If you need those, use tar tf to list the content of the archive.
Use something like:
file="test.tar.gz"
tarfilename=${file%.tar.gz}
read up about it man bash search for variable expansion.