Script to delete text from the end of a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete text from the end of a filename
# 1  
Old 05-16-2009
Script to delete text from the end of a filename

Hi all,

I am trying to write a script that will delete a certain amount of text from the end of a filename.

For example.

HTML Code:
Input:

name of file (more text).pdf

Output:

name of file.pdf
So is it possible to write a script the deletes, in this example twelve (12) digits from the end of the filename whether it be numbers, text, spaces or symbols?


Thanks! Smilie
# 2  
Old 05-16-2009
this thread and this thread , what have you learned from there? show what you have got
# 3  
Old 05-16-2009
ghostdog74,

I've learnt that the simpler way to do this would be to delete a certain amount of text from the end of the filename. This would be the easiest way to do it. I appreciate all the examples that people have given me - they work I just think this would be simpler.

Regards.
# 4  
Old 05-16-2009
To start with the basics..to learn

Code:
a="name of file (more text).pdf"
b=$(echo $a | cut -d. -f1)
c=$(echo $a | cut -d. -f2)
d=${#b)n
wfile=$(echo $a | cut -c -12)
mv $a "$nwfile.$c"

echo after each command and see what does it o/p.


cheers,
Devaraj Takhellambam
# 5  
Old 05-16-2009
Quote:
Originally Posted by Monkey Dean
ghostdog74,

I've learnt that the simpler way to do this would be to delete a certain amount of text from the end of the filename. This would be the easiest way to do it. I appreciate all the examples that people have given me - they work I just think this would be simpler.

Regards.
i am expecting you to at least try and produce some code....because people have shown you how to do that already in those 2 threads.
# 6  
Old 05-16-2009
ghostdog74,

This is what I have so far, running this from /bin/sh

Code:
for file in *.pdf
do 
mv $f `cut -c 12- <<END
$f
END
`
done

# 7  
Old 05-16-2009
Quote:
Originally Posted by Monkey Dean
ghostdog74,

This is what I have so far, running this from /bin/sh

Code:
for file in *.pdf
do 
mv $f `cut -c 12- <<END
$f
END
`
done

if your files always has the structure of "file name (more text).pdf', see here under "substring removal" for examples on string substitution. also get familiar with shell scripting by reading the whole document
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy filepath along with filename at end of another file

Hi , Please help me out for the below problem - I have 2 files in a directory - $ ls -ltr total 4 -rwx------+ 1 abc Domain Users 615 May 31 17:33 abc.txt -rwx------+ 1 abc Domain Users 0 May 31 17:33 ll.sh I want to write the filename of abc.txt along with the directory to the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

3. UNIX for Dummies Questions & Answers

Yum gives error : filename does not end in .rpm.

Hi there /root #yum localinstall sg3_utils Loaded plugins: product-id, refresh-packagekit, subscription-manager Updating Red Hat repositories. Setting up Local Package Process Skipping: sg3_utils, filename does not end in .rpm. Nothing to do /root # above is the error /root... (6 Replies)
Discussion started by: heman96
6 Replies

4. Shell Programming and Scripting

Script to add backslashes to end of certain lines of text

I'd like to write up notes in a relatively readable format and then use a shell script to add LaTeX formatting. Specifically, I'm trying to figure out how to add the LaTeX newline character (\\) to the end of lines without \begin{} or \end{} statements example notes file: \begin{enumerate} --... (2 Replies)
Discussion started by: icskittles
2 Replies

5. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

6. Shell Programming and Scripting

* in end of filename

Hi, i have some files which looks like given below: -rwxrwxrwx 1 amsbatch ams 1608 Oct 22 2009 AMS_BATCH_COMPILE* -rwxr----- 1 amsbatch ams 1608 Oct 22 2009 AMS_BATCH_COMPILE_original* -rwxrwxrwx 1 amsbatch ams 1127 Dec 1 2005 AMS_LMU.sh* drwxr-xr-x 2... (3 Replies)
Discussion started by: mann2719
3 Replies

7. UNIX for Dummies Questions & Answers

* at the end of filename after chmod

Have you seen this before on Solari? Create file -> chmod file-> file gets a * at the end of filename Ex1) gate@server1# ll total 8 drwxr-xr-x 2 stdbgate dba 512 Jan 18 14:30 ./ drwxr-xr-x 3 stdbgate dba 512 Jan 18 14:18 ../ -rw-r--r-- 1 stdbgate dba ... (3 Replies)
Discussion started by: drbiloukos
3 Replies

8. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

9. Shell Programming and Scripting

sed script to remove nth characters from end of filename

Hi all, I have this basic script to remove, in this case 9 characters from the end of a file name. This is what I have so far, for file in *.mov do newname=`echo $file | sed 's/\(.*\)........./\1/' ` mv "$file" "$newname" done The problem is that it removes the file extension as well.... (2 Replies)
Discussion started by: Monkey Dean
2 Replies

10. Shell Programming and Scripting

How to remove a newline character at the end of filename

Hi All, I have named a file with current date,time and year as follows: month=`date | awk '{print $2}'` date=`date | awk '{print $3}'` year=`date | awk '{print $6}'` time=`date +%Hh_%Mm_%Ss'` filename="test_"$month"_"$date"_"$year"_"$time".txt" > $filename The file is created with a... (2 Replies)
Discussion started by: amio
2 Replies
Login or Register to Ask a Question