So, i have a given directory, and that directory contains other subdirectories, and every subdirectory can contain a file. I have to change all the files names, so that it will end in ".txt" .
**You can do something like this using bash : and go over all your directories with it **
for f in *.their_current_end;
do
mv -- "$f" "${f%.their_current_end}.txt"
done
another solution would be using : rename in Linux
rename [OPTIONS] perlexpr files
where the rename will rename all the files mentioned like the regex supplied in the perlexpr parameter.
example :
rename 's/.html/.php/' \*.html
Related
I have for example 001, 001_ses-1, 001_ses-2 folders and files 001_lg1.lsf, 001_recon1.lsf, 001_trac1.lsf in the same directory. I have hundreds of subjects.
I want to move folders: 001_ses-1, 001_ses-2, and files: 001_lg1.lsf , recon1_001.lsf and trac1_001.lsf to 001 main folder. How do I do this?
list={001, 011, 023, 059, ..... 102}
for i in list; do rm i* i; done
so far, I have no clues. Need some help figuring it out!
Given the information provided in the comments, there are two possibilities for identifying the destination folders:
A) Create a manual list. This is how it is done in Bash and compatible Linux shells:
DEST_LIST=( "001" "002" "003" "004" )
for DEST in ${DEST_LIST[#]}; do
mv "${DEST}_"* "${DEST}"
B) Create a regular expression of the pattern and use find.
I will use the pattern from your comment as an example:
PREFIX_PATTERN="[0-9]\{3\}_S_[0-9]\{3\}\.[0-9]"
for DEST in $(find . -type d -regex ".*/${PREFIX_PATTERN}"); do
PREFIX=`expr "${DEST}" : ".*\(${PREFIX_PATTERN}\)"`
mv "${PREFIX}_"* "${DEST}"
The extra line containing the expr command extracts the clean folder name from the full folder path returned by find command.
In a folder with many subfolders, each containing mp4 files with names ending in foobar.mp4
How can I get rid of the suffix? I have tried:
find ./*/*.mp4 -type f -exec rename 's/foobar//' '{}' \;
On surface, there is no need to use 'find', as the rename command has the ability to process list of file. In particular, possible to write
rename 's/foobar//' */*foobar.mp4
It's not clear from the OP what are the actual file names. The above command will rename 'foobar.mp4' to hidden file '.mp4', and the file 'x.foobar.mp4' to 'x..mp4'.
I'm using the Linux rename command line tool to search recursively through a directory to rename any directories as well as files it finds. The issue I'm running into is the rename command will rename a sub-directory of a file then attempt to rename the parent directory of the same file. This will fail because the sub-directory has been renamed resulting in a "No such file or directory"
Command:
rename -f 's/foo/bar/' **
rename -f 's/Foo/Bar/' **
For example, here is an original file that I would like to replace 'foo' with 'bar'
File:
/test/foo/com/test/foo/FooMain.java
Failure:
Can't rename /test/foo/com/test/foo/FooMain.java /test/bar/com/test/foo/FooMain.java: No such file or directory
Preferred File:
/test/bar/com/test/bar/BarMain.java
You can see from the error message that it's attempting to rename the parent directory but at that point the subdirectory has already been changed resulting in the file not found error. Is there parameters for the rename command that will fix this or do I need to go about this in a different way?
Use find + execdir option:
$ find . -execdir rename "s/foo/bar/" {} \;
$ find . -execdir rename "s/Foo/Bar/" {} \;
I have 1000 .mp4 files in a folder in linux and the name of those files are like this:
filename_mywebsite.com_.mp4
I need to rename all files with one command
so how i can change
_mywebsite.com_ to _mywebsite2.com_
in all files
so all my files will be
filename_mywebsite2.com_.mp4
Try renaming it as:
rename 's/.com_/2.com_/' filename_mywebsite.com_.mp4
run below rename command in local directory.
rename 's/mywebsite/mywebsite2/' *.mp4
A piece of code to rename all the files with the same prefix and appends incremental value
declare -i x=1
for f in $(find -type f); do
mv -v $f ${f%/*}/change_me_$x ;
x=$x+1;
done
I would like to rename multiple files (add an extension).. i can use zmv of zsh with
autoload zmv
zmv -n '(**/)(*)' '$1$2.myextension'
but this will rename also all the dirs that are inside the current dir... what can i do to rename only files (recursively) and avoid dirs renaming?
From here:
# Rename names of all files under the current Dir to lower case, but keep Dir names as-is.
$ zmv -Qv '(**/)(*)(.D)' '$1${(L)2}'
so I think the (.D) is what you require. The 'period' indicates matching on regular files (not directories) and the D enables the GLOB_DOTS option.