Shell Script to extract only file name

I have file format like this (publishfile.txt)

drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/OMEGA
drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/sample
drwxrwx---+  h655201 supergroup          0  2019-04-24  09:16  /data/xyz/invisible/se/raw_data/sample(1)

I just want to extract the name OMEGA****, sample, sample(1) How can I do that I have used basename in my code but it doesn't work in for loop. Here is my sample code

for line in $(cat $BASE_PATH/publishfile.txt)
      FILE_PATH=$(echo "line"| awk '{print  $NF}' )
FILE_NAME=($basename  $FILEPATH)

But this code also doesn't wor when used outside for loop

awk -F / '{ print $NF }' "$BASE_PATH"/publishfile.txt

This simply says that the delimiter is a slash and we want the last field from each line.

You should basically never run Awk on each input line in a shell while read loop (let alone a for loop); Awk itself does this by default, much faster and better than the shell.

Get just the filename from a path in a Bash script, How do I extract filename and extension in the Bash shell script from the given Remove .gz from backups.tar.gz file and get backups.tar only: Use the basename command to extract the filename from the path: dirname "/usr/home/theconjuring/music/song.mp3" will yield /usr/home/theconjuring/music. The benefit of the "here string" is that it avoids the need/overhead of running an echo command.

In your code above you have a typo. Your code reads:

    FILE_NAME=($basename  $FILEPATH)

but it should read

    FILE_NAME=$(basename  $FILEPATH)

That should work fine in or outside of a loop

Bash get filename from given path on Linux or Unix, First remove the full file path from input filename. For example if filename input is as /var/log/mail.log then extract full filename ail.log only. Inside a bash script, I need to loop through this list and extract just the date portion of the file name. I managed to figure out how to do it from inside the folder that contains the files using this comman:

Try this:

cat $BASE_PATH/publishfile.txt | awk '{print $7}' | sed 's/.*\///'

the output will be:


UPDATE: I guess cat x.txt | sed 's/.*\///' will still work, if all your files, folders contain at least 1 slash (/).

For the commands used, the manuals are: cat, awk, sed

How to Extract Filename & Extension in Shell Script, What is the smartest way to just extract file name from a full path name. e.g. if I have /usr/sanjay/bin/file_name.c I want only file_name.c Sanjay | The UNIX and� function pathStr { ifHelpShow "$1" 'pathStr --OPTION FILENAME Given FILENAME, pathStr echos the segment chosen by --OPTION of the "absolute-logical" pathname. Only one segment can be retrieved at a time and only the FILENAME string is parsed.

extract only file name from full path file name, Hi, I have a requirement that i need to store only the filename without its extension. Can anyone please help me to do this. For Example, i have stored the � Bash Get Basename of Filename or Directory Name To extract filename and extension in Bash use any one of the following method: basename /path/to/file.tar.gz.gz – Strip directory and suffix from filenames $ {VAR%pattern} – Remove file extension

extracting only the filename without its extenstion, Sometimes the users need to read the basename of the file only by removing the The following `basename` command will retrieve the filename with extension. hi guys i give "ps -ef | grep some_executable_file" on the command line. this "some_executable_file" resides on many paths which r included in the PATH environment variable, so the output depicts only "some_executable_file" in the COMMAND column. how can i get the full path? thanx (3 Replies)

Read filename without extension in Bash – Linux Hint, Normally basename "/dev/user/test.csv" # produce �test.csv� dirname "/dev/user/ test.csv" # produce �/dev/user�. In some shell's (like bash e.g.) you can use� Hey, Scripting Guy! How can I extract just the file name from the full path to the file? — SD. Hey, SD. As you might know, Ted Williams was the last major league baseball player to bat .400 for an entire season.

  • this doesn't work if the file contains something like: drwxrwx---+ h655201 supergroup 0 2019-04-24 09:16 sample (i.e. a filepath without slash / char). You are correct with all other remarks! awk itself does the loop, etc. Using awk or sed is indeed a better solution!
  • Not directly, though it would not be very hard to adapt (try -F [/ \t]')
  • How can I use in a way that file name is separated by comma ? Like sample,sample1
  • See the section Parameter Expansion in the POSIX shell specification. If this doesn't answer your case, please ask a new question here, where you specify exactly the format of your input, and what you want to achieve.
  • If you are asking how to provide that sort o| output, the Awk script can easily be changed to use printf instead of print. If you are asking about input, I absolutely agree with the previous comment.
  • I have tried FILE_NAME=$(basename $FILEPATH) this but it doesn't work
  • Can you post the actual code where you tried that and it didn't work?
  • Properly speaking you should also quote "$FILEPATH" but of course if you kept the buggy for loop it will be too late to actually fix any quoting problems.
  • in the for loop line ?
  • The cat is useless.
  • yes, you can redirect the output, that's true, it may also be a little bit more efficient too. Probably depends how big the file is.