Remove last character from filename

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last character from filename
# 1  
Old 10-03-2011
Remove last character from filename

Hi All,

I have different type of file (.txt,.csv,.xml) format in my current directory. My requirement is that I need to remove the last character from the file format.


Need Output:

How to do that?. Any help is really appreciated.
# 2  
Old 10-03-2011

Try this code,

ls -ltr | awk 'BEGIN{OFS="";}$8 !~ /^$/{a=length($8);b=substr($8,0,a-1);print b;}'

# 3  
Old 10-03-2011
or use sed as its simplest:
ls -lah |sed 's/.$//'

# 4  
Old 10-03-2011
really thanks for quick response and thanks for your timely help
# 5  
Old 10-03-2011
using nawk ..
$ ls -ltr | nawk '{sub(/.$/, "")};1'

# 6  
Old 10-03-2011
You may want to rename the files as a next step, in which case it becomes handy to do it in the shell.

So for example - Put the below into a script to auto-rename the specified files. Otherwise use "ls | while read FILENAME" in stead of the below "ls $* | while read FILENAME"

ls $* | while read FILENAME
  echo Renaming $FILENAME to ${FILENAME%?}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

^M special character in Filename

Hi All, Special character ? is added in between filename. Am not able to figure our why this is happening. In my Development environment special characters are not present. This issue is happening in the higher environment. It would be helpful if somebody can tell what are the possible... (3 Replies)
Discussion started by: weknowd
3 Replies

3. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

4. AIX

removing 8th and 9th character of a FILENAME

Hi everyone, I have a file called pdf.txt which has the following list inside: 7110412_1_31122012.pdf 7286510_4_46667100.pdf 4002176_1_00018824.pdf ... I need a looping script to REMOVE the 8th and 9th character of the filenames listed, no matter what character that is. So... (4 Replies)
Discussion started by: chipahoys
4 Replies

5. UNIX for Dummies Questions & Answers

Display last 8 character of filename

I would like to display the last 8 characters of the filenames for filenames of different lengths. I can delete the last 8 characters with sed but dont know how to only show the last 8 characters. The filenames are something like; afxH340800340000 afxH30800340021 afxR3080034002122 I... (3 Replies)
Discussion started by: Beanz
3 Replies

6. 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

7. Shell Programming and Scripting

move filenames with to another filename without the last character ~

Hi, I need to move filenames with the following format below into filenames without the ~ like sample below. I hope you can help me create a simple unix script that will do this. Thanks in advance! move filename from: AIRS20081225-235641.BSP~ AIRS20081225-235648.BSP~ AIRS20081225-235640.BSP~... (3 Replies)
Discussion started by: ayhanne
3 Replies

8. HP-UX

special character on Filename.. help!!!urgent

I'm trying to rename a file name but the original file has a special character caused by typo. I've tried numerous combination of characters but the file name (original) is still not being recognized which in turn, would not allow me to rename the file. Can someone help? The filename is a... (1 Reply)
Discussion started by: genzbeat
1 Replies

9. Shell Programming and Scripting

Filename character changes

I want to make a script for change filename's character not in English for a given directory. But I am not sure where am I starting from due to I am a little bit new user for scripts. At least is there anybody can help me to make first step ,how can I find illegal or unwanted characters in file... (5 Replies)
Discussion started by: xramm
5 Replies

10. UNIX for Dummies Questions & Answers

rsync problem - space character in filename

I have a freebsd box with a smb connection to a Windows server - I use rsync to copy specific directories, and files, between the two. The problem is the Windows box has files with space characters in their filenames and rsync fails to copy these. Reading the documentation of rsync it appears it... (3 Replies)
Discussion started by: chief2
3 Replies
Login or Register to Ask a Question