I have one file is POLL91.DWN then i want to rename xxxx.yyyymmddhhmmss.POLL91 (xxxx is the store name) it will become
GSTD.20151223121805.POLL91
mv POLL91.DWN GSTD.20151223121805.POLL91
If I understand your question, you can use date and a format string like
mv "POLL91.DWN" "GSTD.$(date +%Y%m%d%H%M%S).POLL91"
Related
How can I pass each one of my repository files and to do something with them?
For instance, I want to make a script:
#!/bin/bash
cd /myself
#for-loop that will select one by one all the files in /myself
#for each X file I will do this:
tar -cvfz X.tar.gz /myself2
So a for loop in bash is similar to python's model (or maybe the other way around?).
The model goes "for instance in list":
for some_instance in "${MY_ARRAY[#]}"; do
echo "doing something with $some_instance"
done
To get a list of files in a directory, the quick and dirty way is to parse the output of ls and slurp it into an array, a-la array=($(ls))
To quick explain what's going on here to the best of my knowledge, assigning a variable to a space-delimited string surrounded with parens splits the string and turns it into a list.
Downside of parsing ls is that it doesn't take into account files with spaces in their names. For that, I'll leave you with a link to turning a directory's contents into an array, the same place I lovingly :) ripped off the original array=($(ls -d */)) command.
you can use while loop, as it will take care of whole lines that include spaces as well:
#!/bin/bash
cd /myself
ls|while read f
do
tar -cvfz "$f.tar.gz" "$f"
done
you can try this way also.
for i in $(ls /myself/*)
do
tar -cvfz $f.tar.gz /myfile2
done
I want to add the ".sbd" after all files ending on ".utf8" in a directory
I do not want to replace the extensions, but really want to add them so the filenames will look like "filename.utf8.sbd"
I think I should adapt the following code, but don't manage to find out exactly how
for f in *.utf8 ; do mv "$f" "$f.sbd" ; done
Can anyone help me? I am very new to the command line
Thanks a bunch!
Your code should work if no file has spaces (or other "special" character) in the name and if the directory is not pathologically big.
In those cases, you can use something like this:
ls|grep '*.utf8$'|while read i; do mv "$i" "$i.sbd"; done
I have picked up a folder name like this:
fname=${i##*/},
Where i is the 'runner' in a for loop, btw. Incidently, the i is the current directory. Now, I would like to use the $fname for renaming an other file. Something like this:
mv OLDFILE.Extension $fname.NewExtension
How can this be done in linux/bash?
Thanks
jd
Thanks. But I did:
mv OLDFILE.Extension $fname"NewExtension"
It worked out well.
jd
I want to change file names in a folder in a way like this:
previous form new form
one-1 to VAS-M0001-001
one-2 to VAS-M0001-002
one-3 to VAS-M0001-003
one-4 to VAS-M0001-004
Can anyone please suggest me a good way to do that?
I would just use a simple loop:
for f in one-*; do mv one-$f VAS-M001-000$f; done
Of course, you can use printf to format the number better (if you have more than 9 files)
rename has such a functionality
[username#hostname aa]$ touch one-1 one-2 one-3 one-4
[username#hostname aa]$ ls
one-1 one-2 one-3 one-4
[username#hostname aa]$ rename one- VAS-M0001-000 one*
[username#hostname aa]$ ls
VAS-M0001-0001 VAS-M0001-0002 VAS-M0001-0003 VAS-M0001-0004
I'm trying to use the rename command in a Terminal in Ubuntu to append a string to the beginning of some avi file names as follows.
rename -n 's/(\w)\.avi$/String_to_add__$1\.avi/' *.avi
So I expect the following:
String_to_add_MyMovie.avi
Problem is that when I run the command it appends the string to the end of the file name, so I end up with the following:
MyMovie_String_to_add_.avi
I'm not sure if I have the perlexpr syntax wrong or something else. Any insight is appreciated.
UPDATE:
Thanks for the suggestions, I tried the suggestions from alno and plundra and made the following modification:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
But now the file gets the string inserted in the middle of the name as follows:
My_String_to_add_Movie
My apologies though, I neglected to mention that the titles are preceded by 3 numeric values, so the file name nomenclature is {3 numbers}-My_Movie.avi so for example 001-My_Movie.avi. But I didn't think this would make a difference since I'm assuming \w+ matches alphanumeric characters, might the '-' be the issue?
Haven't tried Christian's approach yet, I want to be able to use the rename command, or at least understand why it's not working before I try a different approach.
I don't think rename -n is standard. You could do this:
for i in *.avi; do mv $i String_to_add_$i; done
You're only matching a single character with \w, you want \w+, so the complete line would be:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
Correct version:
rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi
You simply forgot + after \w, so it tried to match only one character.