Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am going to agree with ghostdog. There is some hidden character in your filename.
Try navigating to the directory your file is in.
Run "ls -l > myls"
Then "cat -vet myls"
This will show you any hidden characters in your file. See if there is something between readme and your ".". I'll bet there is.
Also, one thing to note about you sed command. It should work but the . in sed is a wildcard so if your file was named readmeAtxt your sed command would change that to readme even though it was not a .txt file because the wildcard matches to the "A". The proper sed is sed 's/\.txt$//' The "\" says ignore the . as a wildcard and treat it as a literal . The $ which could be considered optional says the .txt should occur at the end of the string. If you are dealing with files that have different extensions though, the script is mutch better written with the awk or cut command.
I am going to agree with ghostdog. There is some hidden character in your filename.
Try navigating to the directory your file is in.
Run "ls -l > myls"
Then "cat -vet myls"
This will show you any hidden characters in your file. See if there is something between readme and your ".". I'll bet there is.
Also, one thing to note about you sed command. It should work but the . in sed is a wildcard so if your file was named readmeAtxt your sed command would change that to readme even though it was not a .txt file because the wildcard matches to the "A". The proper sed is sed 's/\.txt$//' The "\" says ignore the . as a wildcard and treat it as a literal . The $ which could be considered optional says the .txt should occur at the end of the string. If you are dealing with files that have different extensions though, the script is mutch better written with the awk or cut command.
Thanks for the explanation. I used this script to get rid of the file extension
$(echo $filename|cut -d'.' -f1)
But still the unknown character is there.
Now when i tried to look at the current directory of the file with the unknown character(rectangle thingy)..it turned out to be a \r char. like this: readme\r
Now when i tried to look at the current directory of the file with the unknown character(rectangle thingy)..it turned out to be a \r char. like this: readme\r
If you have only one such file beginning with readme you can fix it manually with
Code:
mv readme* readme
Here's how I reproduced the problem and fixed it
Code:
c:~/d/tmp$ x=$'readme\r'
c:~/d/tmp$ touch "$x"
c:~/d/tmp$ ls readme*
readme?
c:~/d/tmp$ mv readme* readme
c:~/d/tmp$ ls readme*
readme
breaks on files with spaces... using cat + for loop like that is bad. Either have to change IFS or use a while read loop. Also, with shell, no need to use external command. Its faster that way
Code:
while read -r fullname
do
IFS="."
set -- $fullname
fname=$1
echo $fname
# to get rid of bad chars
echo ${fname//[^[:print:]]/}
done <"file"
Another simple way to do this is to use the command "basename"
For example:-
$ basename filename.txt .txt
will give you :-
filename
basename works on one file and AFAIK does not support wildcard (if it does, correct me). To work on multiple files , effectively a loop is still required. In that case, its the same as calling external command for each file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.