Changing a file extension using rename in OS X - linux

I have a lots of file names under a folder '/files' ,the extesnions of these files are appended with timestamp when that file was created,somthing like abc.csv_20170329. I want to change the extension of all these files to abc.csv_20170330 using the rename command in OSX using the terminal.Can any one help me with the exact command to do that i tried using
$ rename -S '.csv_' .csv_20170330 '.csv'
but this does not work.

for file in abc.csv*
do
mv "$file" "$file__20170330"
done

Related

unziping files in linux folder

in Python I have the following command executed unzip '{dir}ATTOM_RECORDER/*.zip' -d {dir}ATTOM_RECORDER/ as a bash command. The python call works perfectly. my question is about the unzip command itself.
for some reason when unzip is called to expand any relevent zip files in the folder specified, not all the files WITHIN the zip is extracted. There's usually a rpt and a txt file. However, sometimes the txt file is not coming out and I do not have an error command.
How can I ensure the txt file is guaranteed to be extracted before moving on?
Thanks
While you want to unzip your specific zip file. There are many option to decompress any file from zip files. Easiest way is the ā€˜-lā€™ option with unzip command is used to list the contents of a zip file after extracting it.
Syntax: unzip -l [file_name.zip]

How can I move multiple files to a directory while changing their names and extensions using bash?

There are multiple files in /opt/dir/ABC/ named allfile_123-abc allfile_123-def allfile_123-ghi allfile_123-xxx.
I need the files to be named new_name-abc.pgp new_name-def.pgp new_name-ghi.pgp new_name-xxx.pgp and then moved to /usr/tst/output
for file in /opt/dir/ABC/allfile_123* ;
do mv $file /usr/tst/output/"$file.pgp";
rename allfile_123 new_name /usr/tst/output/*.pgp ; done
I know the above doesn't work because $file = /opt/dir/ABC/allfile_123*. Is it possible to make this work, or is it a different command instead of 'for loop'?
This is for the Autosys application in which the jil contains a command to pass to the command line of a linux server running bash.
I could only find versions of each part of my question but not altogether and I was hoping to keep it on the command line of this jil. Unless a script is absolutely necessary.
No need for the loop, you can do this with just rename and mv:
rename -v 's/$/.pgp/' /opt/dir/ABC/allfile_123*
rename -v s/allfile_123/new_name/ /opt/dir/ABC/allfile_123*
mv /opt/dir/ABC/new_name* /usr/tst/output/
But I'm not sure the rename you are using is the same as mine.
However,
since the replacement you want to perform is fairly simple,
it's easy to do in pure Bash:
for file in /opt/dir/ABC/allfile_123*; do
newname=new_name${file##*allfile_123}.gpg
mv "$file" /usr/tst/output/"$newname"
done
If you want to write it on a single line:
for file in /opt/dir/ABC/allfile_123*; do newname=new_name${file##*allfile_123}.gpg; mv "$file" /usr/tst/output/"$newname"; done

How do you add a listing of all the files in your directory to a text file ?

I have a text file in my directory called mytextfile.txt , and I would like to add a listing of all the files in my current directory to the existing contents of that text file.
I tried this :
ls >> `mytextfile.txt`
but this doesn't work . I even tried ls -a but that also doesn't work
You dont need the backticks
ls >> mytextfile.txt

How to rename files according to other files?

I have several files in a folder with extension .img and I have only one file with extension data.txt
What I need is to copy data.txt and rename it as the names of the .img files.
For instance for the first file in my folder:
`Meaurmen_2154_data.img` >>> copy data.txt >>> rename it Meaurmen_2154_data.txt
Now I have :
Meaurmen_2154_data.img
Meaurmen_2154_data.txt ## the content is the same as data.txt
and do the same for all other files. The content of he text files will be the same for all files just we change the name according to the .img files in my folder.
Run this script
#!/bin/bash
imageFiles=( *.img );
for i in ${imageFiles[*]}
do
withoutExtension=${i%.img};
cp data.txt "$withoutExtension.txt";
done
inside the relevant directory and it will do it for you.
Try
for i in *.img; do cp data.txt $i.txt; done
rename 's/.img.txt/.txt/' *.img.txt
In some distro's rename is different, requiring
rename .img.txt .txt *.img.txt
As always, you might find yourself in need of installing additional packages.

linux add a file to an existing file

I have a bash script that runs through several folders and processes the files' content. The resulting file, newFile I want to append to a file in another folder. This file has the same name (newFile). So, I do this
cat $outfn1 >> /newFolder/newFile
But this does not work. The newFile is surely moved to the newFolder. However, it is not appended to the old version of the newFile, which is what I would like it to do. It replaces the old version.
Is there any way around this one?
Thank you.
jd

Resources