Rename file in a directory using shell - shell

Suppose I have some files in a dir called test_dir, like a.sh, b.sh, c.sh, d.bash, and so on. I need to change all the file *.sh to *.bash. How can I achieve it using shell?

Can be done in a single one-liner:
ls *.sh | while read i; do mv "$i" "${i%.sh}.bash"; done

Related

Changing name to files in bash respecting the number associated to each file

I have a bunch of files in bash named myfile1.sh, myfile2.sh, myfile3.sh... and so on. I would like to remove the sh part in each one of them, that is: rename to myfile1, myfile2, myfile3, ... Do you have a suggestion to do it all at once?
Thanks in advance.
for i in *.sh; do mv "$i" "${i%.sh}"; done
If you have the rename command, you can use it:
rename 's/\.sh$//' *.sh
A bash one-liner can be:
for f in *.sh; do mv "$f" "${f%.sh}"; done

Renaming only csv files in a directory using Shell

I'm replacing some contents in the files that have extension .csv in a directory . Is there a way to rename multiple files differently and using sed command.
For example The directory has two file with the following name
data_20050523-20170409.csv
data_20050523-20170409FileHeader.csv
I want to use sed and rename both files as different name for example
new_data.csv or data1.csv
new_data_header.csv or data2.csv
Is it possible to do so in Shell Script ?
DIR=/Users/test/Desktop/NPPES/
cd $DIR
sed 's/","/|/g;s/"/''/g' *.csv
echo Replace completed
#cd $DIR
#rm $FILE
#rm *.pdf
#chmod 777 *.csv
#echo file removed
echo Script completed
Thank you in advance
# Iterate over all csv files in folder
for csv in *.csv; do
# Generate new name with sed
new=`echo "$csv" | sed 's/something/something_else/'`
# Rename csv if new name different
[ "$new" = "$csv" ] || mv "$csv" "$new"
done
But there is also a rename utility, which can do this in one go, using a Perl expression:
rename 's/something/something_else/' *.csv
The problem is that there are 2 rename tools that work completely differently, the one from the util-linux Deb package is not the Perl based one.

Unix shell scripting

I have write shell script to find the particular filename in the directory and create list file to copy the filenames automatically in that list file. But my script is not working list file is not created automatically. I don't know the issue in my script.
Scripts='/app/file'
SrcFiles='/app/file/Mainfiles'
cd "$SrcFiles"
touch SOURCE.LIST
chmod 777 SOURCE.LIST
cd "$Scripts"
cd "$SrcFiles"
for f in *.csv
do
cp -v "$f" /app/file/Mainfiles/SOURCE.LIST/"${f%.csv}"
done
Please try below
search_dir="/app/file/Mainfiles"
for entry in "$search_dir"/*
do echo "$(basename $entry)" >> "/app/file/Mainfiles/SOURCE.LIST"
done

Rename Two files in the Same Folder

Files
events-number1.10a.pdf
Result
events-number1.10a.docx.pdf
Ideal
events-number1.10a.pdf
events-number1.10a.docx.pdf
A simple rename command will do the job.
rename 's/(?=\.pdf$)/.docx/' *.pdf
You can try this simple bash script
#!/bin/bash
for file in *.pdf
do
new_file=$(echo "$file" | sed -r 's/(.*)(\.pdf)/\1.docx\2/')
mv $file $new_file
done
Output:
events-number1.index10a.docx.pdf
events-number1.index10b.docx.pdf
events-number1.index10c.docx.pdf
events-number2.index10a.docx.pdf
events-number2.index10b.docx.pdf
events-number2.index10c.docx.pdf
If you want copy the file using cp command instead of mv command
cp $file $new_file
So your existing files won't change.
Explanation :
Passing all the log file to for loop ,then split the file name to your expected result for using sed command and stored in one variable . Then mv the old file to new file that mean your expected file .

In Unix using shell to rename multiple files

There are many files and names like *.txt; how can I rename all the files to *YYYYMMDD.txt
with a shell script.
Since this is a move operation and could be quite dangerous if done wrong:
Run this first to make sure the script generates correct command
ls *.txt | while read FILE; do echo mv "$FILE" "${FILE/.txt/`date +%Y%m%d.txt`}"; done
Then when you are sure
ls *.txt | while read FILE; do mv "$FILE" "${FILE/.txt/`date +%Y%m%d.txt`}"; done

Resources