Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fish shell: Is it possible to conveniently strip extensions?

Tags:

fish

Is there any convenient way to strip an arbitrary extension from a file name, something à la bash ${i%%.*}? Do I stick to my friend sed?

like image 816
Sergio Losilla Avatar asked Jun 03 '15 09:06

Sergio Losilla


People also ask

Does fish run Bashrc?

Yes, fish is not bash-compatible, so reading . bashrc will typically not work.

Where is fish config?

The configuration file runs at every login and is located at ~/. config/fish/config. fish . Adding commands or functions to the file will execute/define them when opening a terminal, similar to .

Where is fish RC file in Linux?

The settings that would normally be written to a ~rc file are instead located in the config. fish file at ~/. config/fish .


3 Answers

If you know the extension (eg _bak, a common usecase) this is possibly more convenient:

for f in (ls *_bak)
    mv $f (basename $f _bak)
end
like image 198
Evan Benn Avatar answered Oct 09 '22 22:10

Evan Benn


Nope. fish has a much smaller feature set than bash, relying on external commands:

$ set filename foo.bar.baz
$ set rootname (echo $filename | sed 's/\.[^.]*$//')
$ echo $rootname
foo.bar
like image 25
glenn jackman Avatar answered Oct 09 '22 22:10

glenn jackman


You can strip off the extension from a filename using the string command:

echo (string split -r -m1 . $filename)[1]

This will split filename at the right-most dot and print the first element of the resulting list. If there is no dot, that list will contain a single element with filename.

If you also need to strip off leading directories, combine it with basename:

echo (basename $filename | string split -r -m1 .)[1]

In this example, string reads its input from stdin rather than being passed the filename as a command line argument.

like image 22
Claudio Avatar answered Oct 09 '22 20:10

Claudio