0

Using Linux bash I have an exercise in which I have a directory full of files, but their names have all kinds of special characters (some of them start with a number, other with $, other have spaces in the start or in the middle of the names etc.) and they end with .ps

Now I want to select only the first part of their name (until .ps) with all the special characters or numbers they contain.


for file in *.ps; do
    # Use parameter expansion to remove the .ps extension from the file name
    name=${file%.ps}
    # Use the basename command to print the file name without the path
    echo $(basename "$name")
done

or

for file in *.ps; do
    # The for loop will iterate through all the files in the current directory that have a .ps extension.
    # The variable "file" will hold the name of each file in turn.

    # Use awk to print the first field (delimited by .ps) of the file name
    echo $(awk -F.ps '{print $1}' <<< "$file")
done

But with this command some file names are being excluded (for instance those you have spaces in the middle, or starts with $or '

What can I do to contain the whole name file with all the special characters ? Thank you in advance

  • The argument to `echo` needs to be quoted; or simply get rid of the [useless `echo`.](https://www.iki.fi/era/unix/award.html#echo) – tripleee Dec 17 '22 at 17:37

0 Answers0