Renaming only csv files in a directory using Shell - bash

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.

Related

How to read each file in a folder using cat (recursively) and store it in a file?

I have a file with 9k PDF documents. I wish to read the source code (not content) of each of them using cat and store them in a separate text file with the same name as that of the PDF.
For example
cat 030.pdf > 030.txt
I want to do this for all the files in the folder. How can I do it?
Instead of using cat use cp it's more efficient.
find . -name \*.pdf -exec cp {} $(basename {}).txt \;
You could use create a shell script to loop through files in the directory and execute the cat command, and substring the filename to remove .pdf and add .txt:
for entry in "$search_dir"/*
do
if [ -f "$entry" ];then
cat "$entry" > "${entry%%.*}.txt"
fi
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 .

Bash - how do I wipe the contents of all files in a directory

is it possible to wipe the contents of all given files in a directory? e.g. if I have a bunch of .csv files I want wiped.
I generally use "# > .csv" on the command line for a single csv file, but a "# > *.csv" results in a error: bash: *.csv: ambiguous redirect
I have tried piping /dev/null to *.csv but get same result. When I have a directory full of files whose content I want wiped, it's a real pain.
If I use a script and for loop on all the files I get the same error when using the redirect on the $f (the file) in the loop.
Thanks
for f in *.csv; do
> "$f"
done
You can use truncate for the same :
truncate -s 0 *.csv
When you say "wipe" you mean:
"overwite" with random content,
or an simple "truncate"
or even simpler delete?
Delete:
rm *.csv #will delete all .csv file in the currect directory
Truncate
see #John Zwinkcs answer
Overwrite with random and delete
shopt -s nullglob
for file in *.csv
do
echo "wiping $file" >&2
eval "$(gstat -c 'count=%b;blocksize=%B' "$file")"
dd if=/dev/random of="$file" bs="$blocksize" count="$count" 2>/dev/null
rm "$file"
done
This is a way using sed
sed -i '1,$d' *.csv
also
sed -ni '' *.csv

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

How to rename some file of same pattern in shell scripting

I want to write a code is shell scripting which will rename all the files of extension .txt in a current directory to extension .c .Suppose my current directory contains some 100 .txt file. This number is not fixed.
for f in *.txt; do echo mv "$f" "${f%.txt}.c"; done
Remove "echo" when you're satisfied it's working. See the bash manual for the meaning of "%" here.
See man rename. You can rename multiple files providing regexp substitution.
rename 's/\.txt$/.c/' *.txt
If you don't have rename in you system, you can use find:
find . -name '*.txt' | while read FILE; do echo mv "$FILE" "$(echo "$FILE" | sed 's/\.txt$/.c/g')"; done
Remove echo when you verify it does what you want.
awk can do this trick too:
kent$ ls *.txt|awk '{o=$0;gsub(/txt$/,"c"); print "mv "o" "$0;}'|sh

Resources