0

I have the following command that works great:

sudo lsof -w -c smbd -u myuser | grep '.txt'

I just want to get the full path to the file name part of the returned output, the "NAME" column, to put in a report. What is the easiest way to just grab the NAME column?

3
  • read man cut.
    – AlexD
    Jan 9, 2022 at 5:38
  • Not sure if this is the BEST way to do it, but this works: sudo lsof -F -w -c smbd -u myuser | grep '.txt' It appears that the -F splits the output into separate lines per column, and the grep does the job. Happy to hear if there is a better way to do this.
    – NewToLinux
    Jan 9, 2022 at 5:45
  • -Fn will show only the "file name, comment, Internet address". By the way, your command will show processes which are either owned by the user, or whose process name is "smbd". If you're looking only for processes who are both smbd and owned by the user, you should add the -a flag. Except for that, you were pretty close to the answer with the -F flag, you just missed the n afterwards. Submit it as an answer to your own question and accept it.
    – aviro
    Jan 9, 2022 at 9:17

1 Answer 1

0
sudo lsof -w -c smbd -u myuser | tr -s ' ' | cut -d' ' -f 9 | grep '.txt'

tr - replaces repeated blank spaces with a single space. cut - outputs exclusively field\column 9.

1
  • If the file names contains spaces after cut -d' ' -f 9 It will also cut the file name leaving the first part of the filename (filename\ 1->filename) change -f 9 -> -f 9- to correct this behavior. sudo lsof -w -c smbd -u myuser | tr -s ' ' | cut -d' ' -f 9- | grep '.txt'
    – Christian
    Apr 23, 2023 at 4:12

You must log in to answer this question.