Script Help "Line Spliting"
I am trying to create a list of files in a group of directories and chop off the extensions. However when I try it splits into lines. I tried quoting the list "$(ls -R ~joe/Videos)" but then my line cutting off the extension does not work.
I feel like I should knwo what I am doing wrong but I can't seem to figure it out....:banghead: Quote:
All Help is Appreciated !! :cool: |
Using "${file%%.*}" will remove everything right of the first period. Not just the extension if a filename contains multiple periods.
Use "${file%.*}" instead. Another problem may be due to white space in names. A filename with a space in it could be split into two arguments. Here is a howto on different ways to deal with them using for and while loops: http://www.cyberciti.biz/tips/handli...s-in-bash.html So you could use Code:
ls -- . | while read file; do echo "${file%.*}"; done |
Quote:
|
Although...
While I prefer to do everything possible in BASH when scripting (or move it to PERL is I can avoid all exterior calls that way) I may have a useful suggestion. Have you checked the man pages for 'basename' and 'dirname'?
The 'basename' utility specifically allows you to remove KNOWN extensions, the trick being that you must specify the extension. If the extensions you expect constitute a large or ill-defined range then you are in for an interesting amount of string manipulation, and will want to do things more efficiently than you could with basename. Roughly how many files with how many different extensions might we be considering? |
Thanks for the education and suggestions. The while loop method listed works well. I have about 600 files with about 7-10 different extensions so fewer lines of code the better for this small intermittent task.
:cool: ----------Thanks again ----------------- :cool: Quote:
|
Well apart from not being a fan of using ls to parse data (for reasons outline here), something I found a little odd with your script is the following:
Code:
touch /home/$USER/"$Collection-$(date +%F)" Also, in the unusual situation that you run this script around midnight, you could end up with differently named files with your data split over the 2 of them (just a thought) Something you might like to look at with your yes / no question (apart from handling incorrectly entered options) is that you can test for a single response by controlling the format. Example: Code:
read ans |
Thanks for the suggestion on the case statement Grail. I'll hold onto that one for future use. Also thanks for catching the typo.
Works as expected now ........ :cool: Quote:
|
Sorry to still be a bit picky (I am old so I have been told some times it is my prerogative :) ), but your file
construct still has the time issue. My suggestion would be to set it in a variable and use at your leisure or open the file for input at point of creation. Also, redirecting into the file will create it so the touch is not required either. An example of the first option would be: Code:
read Collection |
Thanks for the suggestions grail... :)
|
All times are GMT -5. The time now is 05:50 AM. |