Results 1 to 9 of 9
Hello All,
I am getting one basic problem of cutting last characters in a file name please help me out
the following is the code
#!/bin/sh
file=`ls -t /home/directory | ...
- 11-04-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 45
problem of cutting last two characters..!!!!
Hello All,
I am getting one basic problem of cutting last characters in a file name please help me out
the following is the code
#!/bin/sh
file=`ls -t /home/directory | head -n 1 `
echo $file
output - tr06_2008-11-04_12-09-09_657.34M.jpeg-0
Now my problem is I want to remove the last two -0 character from the file name
there in proper length in tr06 i.e it may vary to tar0006 or tar000008 etc and 657.34M it also varies but the date and time are constant .
so i want = tr06_2008-11-04_12-09-09_657.34M.jpeg

how ?
please help me out ?
- 11-06-2008 #2Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
$file=${file%-0}
should give you what you need.
- 11-06-2008 #3doesn't seem to work. I tried this:Code:
$file=${file%-0}
and got this output:Code:#!/bin/bash file=abcdefgh echo $file $file=${file%-0} echo $file
bash got confused because of the assignment to $file, which should have been an assignment to file. So I ran this instead:Code:abcdefgh /tmp/w1: line 5: abcdefgh=abcdefgh: command not found abcdefgh
and got this output:Code:#!/bin/bash file=abcdefgh echo $file file=${file%-0} echo $file
Not exactly what we wanted, but at least the error message went away.Code:abcdefgh abcdefgh
I'm not sure what you intended with the percent sign, secondmouse, so I just looked at the bash documentation and came up with this:
... and the output looked like this:Code:#!/bin/bash file=abcdefgh echo $file file=${file:0:${#file}-2} echo $file
So that's how to strip off the final two characters of the content of a bash variable.Code:abcdefgh abcdef
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 11-06-2008 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
For the specific case of dash-digit, this can be done:
Producing:Code:#!/bin/bash - # @(#) s1 Demonstrate bash pattern matching, variable string. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) set -o nounset echo echo " Results:" file=abcdefgh echo $file f1=${file:0:${#file}-2} echo $f1 echo my_path=abcdefg-0 echo $my_path f2=${my_path%[-][0-9]} echo $f2 echo my_path=abcdefg-12 echo $my_path f2=${my_path%[-][0-9][0-9]} echo $f2 exit 0
Which may be what secondmouse intended, but Bill's solution is more general ... cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 Results: abcdefgh abcdef abcdefg-0 abcdefg abcdefg-12 abcdefg
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 11-09-2008 #5Just Joined!
- Join Date
- Nov 2008
- Posts
- 15
file=`ls -t /home/directory | head -n 1 |cut -d"-" -f1-5`
or
file=`ls -t /home/directory | head -n 1 |sed -e 's/-0\$//'`
#??
- 11-18-2008 #6Just Joined!
- Join Date
- Feb 2008
- Posts
- 45
Not solved As I expected
Hello All ,
Thanks for the solutions . I am sorry ,I am not able to tell what exactly i want..... !! In my previous threads .
so the solution given by all is 60 % correct for my problem so I expect you all to come another solution that i am going to explain in detail.
Code:#!/bin/bash file=`ls -t /home/directory | head -n 1 ` echo $file # now I have to move the file from /home/directory to /home/anotherdirectory mv /home/directory/"$file" /home/anotherdirectory
the /home/directory as three files as below
tr06_2008-11-04_12-09-09_657.34M.jpeg-0
tr06_2008-11-04_12-09-09_657.34M.jpeg-1
tr06_2008-11-04_12-09-09_657.34M.jpeg-2
I want to move two files from the directory to another directory removing the -0 and -2 from the file name .(don't remove other fields ).
two file to move are
tr06_2008-11-04_12-09-09_657.34M.jpeg-0
tr06_2008-11-04_12-09-09_657.34M.jpeg-2
and to another one as remove from the directory or delete from the directory.
one file to delete
tr06_2008-11-04_12-09-09_657.34M.jpeg-1
how ?
I tried with all the solutios that provided like cutting file=`ls -t /home/directory | head -n 1 |cut -d"-" -f1-5`
and
file=`ls -t /home/directory | head -n 1 |sed -e 's/-0\$//'
But when i want move the file it gives an error
saying mv cannot stat $file no file or directory
obviously...!!!
( because when i remove -0 or -2 then in that directory no filename without -0 and -2..!!!! )
Give as early as possible
thanks
- 11-18-2008 #7Just Joined!
- Join Date
- Oct 2008
- Posts
- 10
mv doesnt move a file from one directory to other; it just changes the file name.
so first change the name, and after that copy the files to destiny directory:
file1="tr06_2008-11-04_12-09-09_657.34M.jpeg-0"
file2="tr06_2008-11-04_12-09-09_657.34M.jpeg-2"
mv $file1 ${file1%-*} #%-* removes the "-0" or "-2"
mv $file2 ${file2%-*}
cp ./$file1 destiny
cp ./$file2 destiny
ciaooooo
- 11-18-2008 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
The command mv means move, and it does move files, provided it is called correctly:
Originally Posted by creepy cripple
There was a notable exception many years ago in early Unix -- directories could not be moved, one needed to tar or cpio them up and then expand them in the appropriate area. That was then and this restriction no longer applies on any modern unix-like system that I have used.Code:`mv' moves or renames files (or directories). Synopsis: mv [OPTION]... SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY If the last argument names an existing directory, `mv' moves each other given file into a file with the same name in that directory. Otherwise, if only two files are given, it renames the first as the second. It is an error if the last argument is not a directory and more than two files are given. -- excerpt from info coreutils mv q.v.
There are situations when a copy (cp) might be appropriate, such as making sure a directory is an exact copy of one from which you are copying, but for most organizing tasks, I use mv ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 11-18-2008 #9Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
drl is right. mv is not just for renaming.
packet, try replacing mv /home/directory/"$file" /home/anotherdirectory with
mv /home/directory/"$file"{-0,-2} /home/anotherdirectory
drl's trimming solution seems to work well
file=`ls | head -n 1`
file=${file:0:${#file}-2}
#after that apply your mv command


Reply With Quote