Find the answer to your Linux question:
Results 1 to 2 of 2
I want to set a number of files of the same type (.ext) equal to the variable FILES without the file extension part of the filename (or then remove the ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Location
    San Francisco, CA
    Posts
    7

    Stripping the extension name (file.ext) from files (set, foreach commands)

    I want to set a number of files of the same type (.ext) equal to the variable FILES without the file extension part of the filename (or then remove the file extensions)...
    e.g.

    $ls

    file1.ext file2.ext file3.ext

    $set FILES=`ls *.ext`

    foreach FILE ($FILES) #now, here FILES = file1, file2, file3 (without file extension)

    Is this possible or is there another way of doing it?

    thanks.

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by tuathan View Post
    I want to set a number of files of the same type (.ext) equal to the variable FILES without the file extension part of the filename (or then remove the file extensions)...
    e.g.

    $ls

    file1.ext file2.ext file3.ext

    $set FILES=`ls *.ext`

    foreach FILE ($FILES) #now, here FILES = file1, file2, file3 (without file extension)

    Is this possible or is there another way of doing it?

    thanks.
    Don't use ls unless there's no alternative.

    For example:

    Code:
    for file in /path/to/*.ext
    do
      i=${file%.*}
      echo "$i"
    done
    Read more about the bash string mangling capabilities on the internet, for example:

    Variable Mangling in Bash with String Operators

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...