Tag Info

New answers tagged

2

I know this doesn't answer your question but in case you were looking for another way to rename the files compared to your work-around loop, why not use find? I have used this command many times to replace file extensions in large directories with hundreds of thousands of files in it. This should work on any POSIX compliant system: find . -name ...


3

When you issue the command: mv *.txt *.tsv the shell, lets assume bash, expands the wildcards if there are any matching files (including directories). The list of files are passed to the program, here mv. If no matches are found the unexpanded version is passed. Again: the shell expands the patterns, not the program. Loads of examples is perhaps best ...


1

For example if you have "asd.txt" and "qwe.txt" files in the directory when you run the command "mv *.txt .tsv" it tries to move these two files into directory named ".tsv". Because there is no such directory, it gives an error.


2

mv *.txt *.tsv doesn't work; mv can rename only one file at a time. You have either misunderstood the explanations or they are wrong. mmv and rename can rename several files at once. But there are two versions of rename around which are called differently. There should be plenty of questions about that here.


1

If you have to cd in and out of several directories then it makes sense to use cd - instead which takes you to the last current working directory. Or you use pushd / popd (in bash). for directory in *; do pushd "$directory" index=1 for filename in *; do extension="${filename##*.}" if [ "$filename" != "$extension" ]; then ...


0

cd /path/to/avidir; mkdir ../avidir2 pax -rwls '|.* - \(..\)x\(.. - \)|Series Title S\1 E\2|' *.avi ../avidir2 The above command will create hard-links to all of your files in another directory. This enables you to verify all of the changes are to your satisfaction before removing the files with the unwanted names.


1

You are close, but forgot(?) to put $ at variable expansion, try: for f in *01x*; do mv -v -- "$f" 'Series Title S01 E'"${f#*01x}"; done Notice single quotes in target file due to spaces in filename.


0

With zsh, first load the zmv function with autoload zmv (you can do this from your .zshrc) then run zmv -w '*.old' '$1' or zmv '(*).old' '$1'


-2

"Refinement" of Christian's solution ( http://unix.stackexchange.com/a/180275 ) using rev: ls -1 | while read line; do mv "$line" "`echo $line | rev | cut -d'.' -f2- | rev`"; done


0

You didn't say what operating system you are using, but many linux distributions have a 'rename' command that you can use for this. There are actually two quite different rename commands - Debian and similar systems supply one while Redhat and similar supply another - but either one will work here. Perl based 'rename' on Debian, Ubuntu etc: ...


-1

Assuming "file" does not contain a "." : ls -1 | while read line; do mv "$line" "`echo $line | cut -d'.' -f1,2`"; done This example splits the filename by period and with -f you can control the fields to keep. For your third example, you would use: ls -1 | while read line; do mv "$line" "`echo $line | cut -d'.' -f1,3`"; done


1

If you want to restrict answers to shell (Bash) programming (rather than using a renaming tool), try this: for oldname in *.old ; do newname="${oldname%%.old}" if [ -e "$newname" ] ; then echo "Cannot rename $oldname because $newname exists." >&2 else mv -- "$oldname" "$newname" fi done I use quotes around $oldname ...


2

There are a lot of multi rename tools like http://file-folder-ren.sourceforge.net/ But I think the fastest way to rename is a simple script like: for i in *.old do mv -- "$i" "${i%.old}" done Note there is no error checking and if the target file exists it might be overwritten.


4

Use bash's parameter substitution mechanism to remove matching suffix pattern: for file in *.old; do mv -- "$file" "${file%%.old}" done


0

Using the same rename command as @sputnick, combined with find: find /path/to/collection -type f -execdir rename -n '$_ = lc;y/ /-/' {} + $_ = lc converts the input to lowercase. y/ /-/ changes spaces to - (similar to tr). -execdir runs the command in the file's directory, so that {} is replaced with ./name-of-file. Therefore directories should remain ...


1

You can install the Perl script rename. Then try doing this : $ rename -n 's/[A-Z]/lc($&)/ge; s/\s/_/g' files* (remove the -n switch when your tests are OK) There are two utilities called rename. The one in Fedora can't do this. Some other distributions come with the Perl one by default. If you run the following command (GNU) $ file ...


0

You can try with below command. $ find -type f -exec bash -c 'mv $0 $(echo $0|sed "s/\//_/2")' {} \; Above command will move the files and the directory will remain which can be removed later. $ ls foo1_bar1.txt foo1_bar2.txt foo2_bar3.txt foo2_bar4.txt foo3_bar5.txt foo3_bar6.txt


1

with pax... pax -Xrwls '|/|_|g' */ "$PWD" That will create a hardlink in the current directory to all files in its child directories with a _ substituted for /. You can then inspect the results and remove all child directories with... rm -rf */ ...once you have verified the results are to your liking.


3

The mv command calls the rename system call, which is guaranteed to be atomic. However, there are two exceptions: If the source and the destination are on different filesystems, which is relatively common for /home vs. /tmp, then rename fails, and mv then works by copying the source tree to the destination and then removing the source tree. This is ...


3

Using perl's rename : find . -depth -type f -exec rename 's@(?<!\.)/@_@g' -- {} \; OUTPUT $ find -type f ./foo2_bar4 whit a space.txt ./foo3_qux1_bar6.txt ./foo2_bar3.txt ./foo3_qux1_bar5.txt ./foo1_bar2.txt ./foo1_bar1.txt NOTE I use a negative look behind (?<!\.) to don't touch the first ./ I keep the empty dirs, feel free to : find . -depth ...


4

find */ -type f -exec bash -c 'file=${1#./}; echo mv "$file" "${file//\//_}"' _ '{}' \; remove echo if you're satisfied it's working. Don't worry that the echo'ed commands don't show quotes, the script will handle files with spaces properly. If you want to remove the now empty subdirectories: find */ -depth -type d -exec echo rmdir '{}' \;


2

If the directories are on the same hardware partition mounted as a single filesystem, then moving something is actually just renaming it to a different path. However, if they are not, then each file inside may need to be read in and copied out, so no part of the move would be atomic. As Gilles points out, POSIX stipulates this is the case for discrete ...


1

Here is an alternative way should rename happen to be missing: find 0[0-6][0-9] -name "*.jpg" -exec sh -c 'for i do echo mv "$i" "${i%g}eg"; done' sh {} +


3

In the first instance, * is expanded by the shell before it gets to rename (if it is expanded at all - I doubt anything matches {}/*), and if it isn't expanded, the command that is executed is rename with the three arguments -n, s/jpg/jpeg/ and some/path/*. That last argument is not the name of an existing file so rename does nothing. Without shell ...


0

You should not specify type d unless you want to rename only directories. To change extensions .jpg to .jpeg try find . -maxdepth 2 -mindepth 2 -name '*.jpg' -exec sh -c 'echo mv -- "$0" "${0%%.jpg}.jpeg"' {} \; Remove echo if you like what you see on the screen.


0

find | rename 's/\.jpg$/.jpeg/' or if you have oder files in the currunt directory find 0[0-9][0-9] | rename 's/\.jpg$/.jpeg/'



Top 50 recent answers are included