After extracting files with 7zip how can you rename those file and save - linux

I am using the following command to extract the files with 7zip
7za x -p$passwd $file -o$outdir
there are many files getting extracted, i want to rename these files after extraction how can i do it with help of writing a script in ksh
files=`ls ABC_0722*.zip | xargs -r`
outdir="/abc/def/prq/xyz"
for file in $files; do
passwd=`echo $file| awk '{print substr($0,11,2)}'``echo ABC``echo $file| awk '{print substr($0,5,2)}'`
7za x -p$passwd $file -o$outdir
done
After the extraction I need to rename the files to abcdef.

After decompress all files, use a for loop looking for all normal files that are not symbolink links, replace the basename of the path and replace it with abcde plus a counter:
for f in $outdir/*; do
[[ -f $f && ! -L $f ]] && { ((++i)); mv -- "$f" "${f%/*}/abcde$i"; };
done

Related

Rename files that have consecutive numbers for extensions

I have a directory with a few hundred files in the following format:
file.txt.1
file.txt.2
file.txt.3
file.txt.4
...
I need to rename these all to this format:
file1.txt
file2.txt
file3.txt
file4.txt
...
Use mmv, install sudo apt-get install mmv.
$ mmv -n '*.*.*' '#1.#3.#2'
file.txt.1 -> file.1.txt
file.txt.2 -> file.2.txt
file.txt.3 -> file.3.txt
Or use find and shell (POSIX sh/bash/Korn/zsh) parameter substitution expansion.
find . -type f -execdir sh -c 'num=${1##*.}; echo mv -v "$1" "file.${num}.txt" ' _ {} \;
Remove echo to perform actual rename on files.
Not sure if this is the best way to do what you're asking but it will rename all the files the way you need. Just save it to a file (rename.sh for example) then give it execution permissions (chmod +x rename.sh) and run with ./rename.sh
#!/bin/bash
for filename in file*; do
newFile=`echo $(basename $filename) | awk -F'.' '{print $1 $3 "." $2}'`
echo mv \"$filename\" \"$(dirname $filename)/$newFile\";
done | /bin/bash
If you wanted to run a dry-run, replace | /bin/bash with > renames.txt. This will save all the renamed files to the text file where you can review the changes.

remove prefix and suffix from the file with mv

I have a directory which contains multiple files. I need to rename these files.
This is how the file names looks like:
snap-file-name-1.txt
snap-file-name-2.txt
snap-file-name-3.txt
I need to remove "snap" and ".txt" from these files.
-file-name-1
-file-name-2
-file-name-3
How do I do that with mv command?
Use sed to manipulate the file name:
ls | while read file; do
mv -- ${file} $(sed -n 's/snap\(.*\).txt/\1/p' <<<${file})
done
With Bash you could do something like this to rename those files:
#!/bin/bash
files=$(find -type f -name 'snap-file-name-*.txt')
for f in $files
do
mv "$f" "$(echo $f | sed -n 's/snap\(.*\).txt/\1/p')"
done
Use rename command with specific regex pattern:
rename 's/snap([-a-z0-9]+)\.txt$/$1/' *.*

rename files to remove extension on multiple files in linux using a find and exec command

I have several file that I need to rename. I want to rename to remove the extension on all file (file without extension). I'll prefer a "find and exec" command in Linux but I'm open to other solutions. Thanks
Have
file.txt
file1.txt
file2.txt
file3.
Want
file
file1
file2
file3
You can rename command:
rename 's/\.[a-z]*$//g' file*
If you want to find all files in directory and subdirectories then you can use find command:
find . -name 'file*' | xargs rename 's/\.[a-z]*$//g'
Depending on the files' possible names, you can adjust the regex in the rename command.
A bash solution would be:
for fname in *.txt; do
[[ ! -e ${fname} ]] && continue
new=${fname%.*}
mv "$fname" "$new"
done
The line [[ ! -e ${fname} ]] && continue to guard against the scenario when there's file with .txt as extension.

Change extension of file using shell script

How to change extension of all *.dat files in a directory to *.txt.
Shell script should take the directory name as an argument. Can
take multiple directories as arguments. Print the log of command
result in appending mode with date and timestamp.
Bash can do all of the heavy lifting such as extracting the extension and tagging on a new one. For example:
for file in $1/*.dat ; do mv "$file" "${file%.*}.txt" ; done
Batch File Rename By File Extension in Unix
# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done
# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done
#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done
#change .html files to php
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done
so ==>
# change .dat files to .txt
for file in *.dat ; do mv $file `echo $file | sed 's/\(.*\.\)dat /\1txt/'` ; done
#!/bin/bash
for d in $*; do
for f in $(ls $d/*.dat); do
echo $(date) $(mv -v $f ${f%.dat}.txt)
done
done
Output redirection should be done by the shell when running the script
Leaving out argument validity checks
Simple script:
#!/bin/bash
if [ $# -lt 2 ] then
echo "Usage `basename $0` <any number of directories space separated>"
exit 85 # exit status for wrong number of arguments.
fi
for directories
do
for files in $(ls $directories/*.dat); do
echo $(date) $(mv -v $files ${files%.dat}.txt)
done
done
The first for loop by default loops on the $# i.e. command-line arguments passed.
Follow Pben's solution, if your filename contains blank space, you should use double quotation marks to the variable like the following:
#remove the space in file name
#example file name:19-014-0100.mp3 .mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ;
do target=`echo "$file" | sed 's/ //g'`;
echo "$target";
mv "$file" "$target";
done;
#remove the duplicate file extension in file name
#example file name:19-014-0100.mp3.mp3
#result file name:19-014-0100.mp3
$ for file in *.mp3 ;
do target=`echo "$file" | sed 's/\.mp3\.mp3$/.mp3/g'`;
echo "$target";
mv "$file" "$target";
done;
To rename (changing extention) all my html files on epub files I use this command line :
find . -name "*.html*" -exec rename -v 's/\.html$/\.epub/i' {} \;
Script, first finds the names of the given extensions.
It removes the extension from names. Then adds backslash()
for identification of terminal.
Then the 'mv' command executed.
Here the '.temp' folder is used to hide the process from user,
in GUI.
#!/bin/sh
if [ $# -ne 3 ]
then
echo "Usage: ./script folder current_extension modify_extension"
exit
fi
mkdir .temp
find $1 -name "*.$2" > .temp/output_1 && sed "s/$2//" .temp/output_1 > .temp/output_2 && sed -e "s/[ \t]/\\\ /g" .temp/output_2 > .temp/output_3
while read line
do
mv -v "$line""$2" "$line""$3"
done < .temp/output_3
rm -rf .temp
The output files are saved inside the '.temp' folder,later the '.temp' folder is removed.
The top voted answer didn't really work for me. I may have been doing something wrong. My scenario was trying to create a file with the original name, but with the date appended to it, along with changing the extension from .xslx to .csv. This is what worked for me:
csvname=`echo $xlsx |sed 's/\.xlsx//'`"-$now"`echo $xlsx | sed 's/\(.*\.\)xlsx/\.csv/'`
So, for all the .dat files in a directory (without the date addition), you could run something like this:
for i in *.dat
do mv $i `echo $i |sed 's/\.dat//'``echo $i | sed 's/\(.*\.\)dat/\.txt/'`
done
From the above, this section of code just removed the extension:
echo $i |sed 's/\.dat//'
And this section changes the .dat to .txt:
echo $i | sed 's/\(.*\.\)dat/\.txt/'
And by bumping them next to each other, it concatenates the two outputs into the filename. It's like doing this:
mv [filename][.dat] [filename] + [.txt]
Though, I did use STDOUT instead of the 'mv' command.
Following command to change file extention .c to .h
find . -depth -name "*.c" -exec sh -c 'dname=$(dirname {}) && fname=$(basename {} .c) && mv {} $dname/$fname.h' ";"
change js to cjs extension files recursively:
cd dist # where you place your .js
for file in $(find . -type f -name "*.js"); do mv "$file" "${file%.*}.cjs"; done

Add file extension to files with bash

What is the good way to add file extension ".jpg" to extension-less files with bash?
# Strip .jpg from all filenames
for f in *.jpg; do mv "$f" "${f%.jpg}"; done
# Add .jpg to all filenames (even those with .jpg already)
for f in *; do mv "$f" "$f.jpg"; done
# Add .jpg to all filenames...unless they are already .jpg
for f in *; do case "$f" in *.jpg) echo skipped $f;; *) mv "$f" "$f".jpg; esac; done
# Add .jpg to all filenames...unless they already have a . extension
for f in *; do case "$f" in *.*) echo skipped $f;; *) mv "$f" "$f".jpg; esac; done
You can use rename:
rename 's/(.*)/$1.jpg/' *
Another way - without loops
find . -type f -not -name "*.*" -print0 |\
xargs -0 file |\
grep 'JPEG image data' |\
sed 's/:.*//' |\
xargs -I % echo mv % %.jpg
Breakdown:
find all files without extension
check the file type
filter out only JPG files
delete filetype info
xargs run the "mv" for each file
the above command is for dry run, after it you should remove the "echo" before mv
EDIT
Some people suggesting that here is needed "Wrap path arguments in quotes; avoids argument splitting on paths with spaces".
Usually, this recommendation is true, in this case isn't. Because, here the % is got replaced not by shell expansion but by the xargs internally (directly), so the % will be substituted correctly even with spaces in filenames.
Simple demo:
$ mkdir xargstest
$ cd xargstest
# create two files with spaces in names
$ touch 'a b' 'c d'
$ find . -type f -print
./c d
./a b
# notice, here are spaces in the above paths
#the actual xargs mv WITHOUT quotes
$ find . -type f -print | xargs -I % mv % %.ext
$ find . -type f -print
./a b.ext
./c d.ext
# the result is correct even in case with spaces in the filenames...
Simple,
cd to the directory where your files are and:
for f in *;do mv $f $f.jpg;done
dry run:
rename -n s/$/.jpg/ *
actual renaming:
rename s/$/.jpg/ *
find . | while read FILE; do if [ $(file --mime-type -b "$FILE") == "image/jpeg" ]; then mv "$FILE" "$FILE".jpg; fi; done;
In my case i was not aware of the filetype so i used the mv command with the help of the file command to examine and possibly find the file type. This solution might not be perfect for all files since the file command might not recognize the filetype but it worked mostly good for me.
for f in *; do ext=$(file $f | awk '{print $2;}'); mv -n "$f" "$f.$ext"; done
The use of awk is to strip the second word of the string returned from the command file that is actually the extension.
rename --dry-run * -a ".jpg" # test
* -a ".jpg" # rename
You can use move multiple files. I am a maintainer of this project. The syntax is simple.
mmf files*
It will open your $EDITOR with all files names, or vim by default and you can simply highlight the end of all file names using Ctrl+v+G in vim , save the file,quit and that it , all your files are renamed
Ryan Li
The correct syntax for adding a file extension to multiple files within a directory which do not have a file extension is
find . | while read FILE; do if [[ -n `file --mime-type "$FILE" | grep 'message/rfc822'` ]]; then mv "$FILE" "$FILE".eml; fi; done;

Resources