[Shell] Get the file name and extension

Not much to say, look at the code experience.

[oracle@master test]$ cat test.sh
#!/bin/bash
file=test.sh
Echo "filename: ${file%.*}"
 Echo "extension: ${file#*.}"

[oracle@master test]$ sh test.sh
 File name: test
 Extension: sh


[oracle@master test]$ cat test.sh
#!/bin/bash
file=test.tar.gz
 Echo "filename: ${file%%.*}"
 Echo "extension: ${file##*.}"

[oracle@master test]$ sh test.sh
 File name: test
 Extension: gz

 

Intelligent Recommendation

Parse the file path, get the file name and extension

_splitpath function    In C or C++ programming, the path to obtain the program or file is often used, and the paths are split and merged. _splitpath and _makepath can accomplish this function.    Func...

PHP get file extension suffix name

     PHP to get the file extension? Original address: http://www.51ask.org/question/226    ...

How do I get the file name extension?

lastIndexOf: is to search from right to left, return the index corresponding to the target element after finding the target element; return -1 if the target element is not found; In the programming, y...

More Recommendation

go get the file name or extension language

go get the file name or extension language...

python: Get the suffix (extension) / file name

method Useos.path.splitext(file)[0] Availablefile name 。 useos.path.splitext(file)[-1] Can get. The beginningFile extension 。 code...

java new file, get the file name, file extension get

Code section: Output section:  ...

Shell MV modify file name, extension, folder name

Format:$ MV File/Folder Name New File/Folder Name Modify the file suffix in batches Batch modification file name Modify the folder name...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top