0

I have a c shell script which has the following two lines, it creates a directory and copies some files into it. My question is the following - the files being copied look like this abc.hello, abc.name, abc.date, etc... How can i strip the abc and just copy them over as .hello, .name, .date.. and so forth. I'm new to this.. any help will be appreciated!

mkdir -p $home_dir$param    
cp /usr/share/skel/* $home_dir$param
Trevor
  • 1,111
  • 2
  • 18
  • 30
sparta93
  • 3,684
  • 5
  • 32
  • 63

1 Answers1

1

You're looking for something like basename:

In Bash, for example, you could get the base name, file suffix like this:

filepath=/my/folder/readme.txt
filename=$(basename "$filepath")  # $filename == "readme.txt"
extension="${filename##*.}"       # $extension == "txt"
rootname="${filename%.*}"         # $rootname == "readme"

ADDENDUM:

  1. The key takeaway is "basename". Refer to the "man basename" page I linked to above. Here's another example that should make things clearer:

    basename readme.txt .txt # prints "readme"

  2. "basename" is a standard *nix command. It works in any shell; it's available on most any platform.

  3. Going forward, I would strongly discourage you from writing scripts in csh, if you can avoid it:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190