- How to only get file name with Linux ‘find’?
- How to Get Filename from the Full Path in Linux
- Using Basename Command
- Using Bash Parameter Substitution
- How to Get Filename From Path in Bash?
- Method 1: How to Get File Name From the Path Using Bash script?
- Method 2: How to Get File Name From the Path Using Basename Command?
- Syntax:
- Bonus Tip: How to Get and Remove Extensions From the File Name?
- Conclusion
How to only get file name with Linux ‘find’?
In GNU find you can use -printf parameter for that, e.g.:
find /dir1 -type f -printf "%f\n"
@Urchin No reason it shouldn’t so long as you have correct logic (i.e. -o has lower precedence than implied -a , so you will often want to group your -o arguments)
I don’t think this is the right answer. find -printf «%f\n» -exec echo <> \; reveals that the original filename is passed forward
If your find doesn’t have a -printf option you can also use basename:
find ./dir1 -type f -exec basename <> \;
Use -execdir which automatically holds the current file in <> , for example:
find . -type f -execdir echo '<>' ';'
You can also use $PWD instead of . (on some systems it won’t produce an extra dot in the front).
If you still got an extra dot, alternatively you can run:
find . -type f -execdir basename '<>' ';'
-execdir utility [argument . ] ;
The -execdir primary is identical to the -exec primary with the exception that utility will be executed from the directory that holds the current file.
When used + instead of ; , then <> is replaced with as many pathnames as possible for each invocation of utility. In other words, it’ll print all filenames in one line.
If you are using GNU find
Or you can use a programming language such as Ruby(1.9+)
If you fancy a bash (at least 4) solution
shopt -s globstar for file in **; do echo $; done
If you want to run some action against the filename only, using basename can be tough.
find ~/clang+llvm-3.3/bin/ -type f -exec echo basename <> \;
will just echo basename /my/found/path . Not what we want if we want to execute on the filename.
But you can then xargs the output. for example to kill the files in a dir based on names in another dir:
cd dirIwantToRMin; find ~/clang+llvm-3.3/bin/ -type f -exec basename <> \; | xargs rm
find /dir1 -type f -exec basename <> \;
As others have pointed out, you can combine find and basename , but by default the basename program will only operate on one path at a time, so the executable will have to be launched once for each path (using either find . -exec or find . | xargs -n 1 ), which may potentially be slow.
If you use the -a option on basename , then it can accept multiple filenames in a single invocation, which means that you can then use xargs without the -n 1 , to group the paths together into a far smaller number of invocations of basename , which should be more efficient.
find /dir1 -type f -print0 | xargs -0 basename -a
Here I’ve included the -print0 and -0 (which should be used together), in order to cope with any whitespace inside the names of files and directories.
Here is a timing comparison, between the xargs basename -a and xargs -n1 basename versions. (For sake of a like-with-like comparison, the timings reported here are after an initial dummy run, so that they are both done after the file metadata has already been copied to I/O cache.) I have piped the output to cksum in both cases, just to demonstrate that the output is independent of the method used.
$ time sh -c 'find /usr/lib -type f -print0 | xargs -0 basename -a | cksum' 2532163462 546663 real 0m0.063s user 0m0.058s sys 0m0.040s $ time sh -c 'find /usr/lib -type f -print0 | xargs -0 -n 1 basename | cksum' 2532163462 546663 real 0m14.504s user 0m12.474s sys 0m3.109s
As you can see, it really is substantially faster to avoid launching basename every time.
How to Get Filename from the Full Path in Linux
The full path of a file in Linux refers to the complete address including directories and subdirectories using which the file can be located. Every operating system has different variants of the full path of a file.
In Linux, it looks something like the following:
This is the full file path of file test.txt.
When manually dealing with a file path, it is easy to get the filename, but if you are using a script for some kind of automation, you might need to extract the filename in certain scenarios.
Let’s see how we can extract filename out of the full path in Linux.
Using Basename Command
The whole purpose of the basename command is to retrieve filename out of a file path. Although it is not too difficult to get the filename from a full path, basename automatically does it for you and you don’t have to go through the trouble of parsing the file path, etc.
$ basename full_file_path $ basename /var/log/syslog [example command]
Use the -a an argument to pass multiple paths.
$ basename -a /var/log/syslog /var/log/dpkg.log
Using Bash Parameter Substitution
Bash Parameter Substitutions are a way to modify a variable on the fly. Hence to use this method, we need to store the file path in a variable.
Now we will apply parameter substitution and store the value in another variable.
Basically what this does is that: it shreds the part before the last ‘/’ in ‘full path’ and just keeps the rest of the string, which is nothing but the filename.
Conclusion
In this article, we learned about two ways to get a filename from a full file path in Linux. If you want to learn more about the command basename and parameter substitutions in Bash, make sure you go through their respective man pages with:
Thanks for reading and let us know your thoughts and questions below!
How to Get Filename From Path in Bash?
In Linux, bash script is a shell in which commands are written in sequence. Users can write anything in the bash script, such as programs and commands, and run it to get the file’s output. Using this user can also get file names from the path. In this post, you will learn to get file names from the path using the bash script. The post’s content is as follows:
Method 1: How to Get File Name From the Path Using Bash script?
The file name can easily be obtained using the Bash script. Create a bash script file and type the script into the file, make the file executable, and run that file to get the output. Let’s implement and learn this method in the following steps.
Step 1: Create the Bash Script File
Create the file with the .sh extension using the touch command. To create the file is obtained as follows:
The file will be created, as can be seen in the above image.
Step 2: Make the File Executable
Make executable using the “chmod” command. To do so, the following command will be used:
To verify that file is executable, run the following command in the terminal:
The above image shows the executable permission for the user.
Step 3: Type the Script
Now, you need to type the script inside a bash shell of the file by typing the below command and pressing enter:
After entering, type the below script and save the file:
#!/bin/bash filepath="/home/linuxfoss.txt.gz" filename=$ echo $filename
You can also open the file manually for typing the script:
Let’s move to the next step.
Step 4: Run the Bash Script File
Run the Bash file in the terminal to check the output. To do so, the following command is useful:
The above image has displayed the file name.
Note: File name can also be used by the terminal through bash script parameters. To do so, execute the following command one by one:
$ filepath="/home/linuxfoss.txt.gz" $ filename=$ $ echo $filename
Let’s move to the second method to get the file name.
Method 2: How to Get File Name From the Path Using Basename Command?
The Basename command is used to print the base path (excluding the parent path), which is useful in the bash script of the file where the user can extract the file name. The syntax for the basename command is given below:
Syntax:
Let’s check the base command in the bash script:
Open your bash file with nano editor. To do so, execute this command in the terminal:
Once you enter the above command, this will take you to the editor. Now, add the command in the script:
#!/bin/bash Basename /home/linuxfoss.txt.gz
Save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X”.
Run the bash script to check the file output:
The output shows that the file name has been retrieved from the path.
For multiple paths, the following command will be used; we are using three paths to get file names:
#!/bin/bash basename -a /home/foss.txt /home/linuxfoss.txt.gz /home/get-pip.py
Put this command in the bash script file:
Save and run the file to check the output:
All three file names have been displayed, as shown in the above image.
Bonus Tip: How to Get and Remove Extensions From the File Name?
You can also get the extension from the bash script from the filename. For this feature, type the following script into the bash file:
#!/bin/bash filepath="/home/linuxfoss.txt.gz" echo "$"
After that, run the bash file using the following command:
The above command will display only the extension of the file name, which can be seen in the above image.
Conclusion
In Linux, file names can be obtained using the bash file through a bash script, bash script parameters, or the basename command. In this article, all the possible ways to get file names from the path have been demonstrated. Apart from that, methods to get an extension from the file name or remove the extension from the file name have also been illustrated in this post.