Linux find only name

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.

Читайте также:  Linux show tree directory

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 only the file name using find command on Linux?

Linux find statement is one of the most widely used statements that allows us to walk a file hierarchy. It is used to mostly find a specific file or directories and we can also append different other Linux statements or flags along with it to enhance or do a complex operation.

Let’s explore an example of a find statement to understand it better.

In the linux code shown below I am trying to search for a file inside my Downloads folder, and for that I am making use of the find statement.

Output

Notice that if the find command is able to locate the file then it will simply print the name of the file, if not then it will not return anything the terminal process will terminate.

Now we know how find statement works, let’s explore the case where we only want to get the names of the files and not everything that comes with it.

If you are making use of GNU terminal, then the command shown below will help you to achieve just that.

Just write the command to your GNU terminal

Output

immukul@192 ~ % find ./ -type f -printf "%f
" Applications C: emp1 Desktop Documents Downloads Exercism IdeaProjects Library MEGAsync MEGAsync Downloads Movies Music Pictures Public PycharmProjects

It should be noted that if you are not on a GNU terminal, then you might get an error stating that -printf is not found. In that case, you can make use of a bash script that helps us to achieve the same output.

Consider the script code shown below −

for file in **; do echo $; done

Just save the above code in a file ending with a .sh extension and then give the file permission using the following command −

Finally execute the shell script, using the following command −

Читайте также:  Service restart command in linux

Output

immukul@192 ~ % ./out.sh Applications C: emp1 Desktop Documents Downloads Exercism IdeaProjects Library MEGAsync MEGAsync Downloads Movies Music Pictures Public PycharmProjects

Источник

How to Get File Name Only With Linux Find Command

How to Get File Name Only With Linux Find Command

The Linux find command is a powerful tool with many options. However, the output always displays the full path of the file. Sometimes it is desirable to only get the filename itself. In this Linux quick tip we will discuss some methods for formatting the output to show the file name only.

Let’s take a look at an example of find output. Here we are searching for a song named Mother in the Music directory.

[[email protected] ~]$ find ~/Music -name Mother /home/mcherisi/Music/Pink_Floyd/Mother

As you can see, it found the file and returned the full path. But, what if we only wanted the file name itself? Well as with anything in Linux you have a few (actually many) options. Let’s examine just a few.

Strip The Directory Information From Output with Basename

The first thing that comes to my mind is the basename command. This shell utility is designed specifically to remove the path from a file name. In the following example we will use command substitution to pass the output of the find command to basename.

[mc[email protected] ~]$ basename -a $(find ~/Music -name Mother) Mother

In the above example we used basename with the -a option. This ensures that all results are displayed if multiple files are found.

Instead of using command substitution, we can also use basename with -exec .

[[email protected] ~]$ find ~/Music -name "M*" -exec basename <> \; Music Mother Mean_Street

Using Printf to Format the Output of Find Command

Another option is to use find’s built in -printf option. Here we are using the %f format specifier followed by \n (newline).

[mc[email protected] ~]$ find ~/Music -name Mother -printf "%f\n" Mother

For more information about the find command, read our primer called «Linux Find Command — Search for Files on the Linux Command Line»

Источник

command to find files by searching only part of their names?

Do you want to list matching files in only the current directory? in any directory at or below the current directory? anywhere on the system?

6 Answers 6

Finding Files with bat Anywhere

To find all files anywhere inside /path/to/folder whose names contain bat , you can use:

find /path/to/folder -name '*bat*' 

I have quoted the search pattern *bat* because, if the quotes were omitted and files match *bat* in the current directory, the shell will expand *bat* into a list of them and pass that to find . Then find wouldn’t work right. ( \*bat\* and «*bat*» also work.)

To search in the folder you’re currently in (e.g., that you’ve cd ed to), use . , as usual:

To search your whole computer, use / . To search your home directory, use ~ , or the full name of your home directory. (The shell expands ~ to your home directory’s fully qualified path.)

Читайте также:  Hello world ассемблер linux

Broadening or Narrowing Your Search, Based on Name

If you want to search case-insensitively, so files containing BAT , bAt , and so forth are matched, use the -iname test instead of the -name test:

find /path/to/folder -iname '*bat*' 

I’ve noticed all your files end in .c . If you only want to find files like that, use:

find /path/to/folder -name '*bat*.c' 

I noticed all your filenames have bat either at the very beginning or the very end of the part preceding the .c suffix. If you want to avoid matching files like embattled.c , you could use:

find /path/to/folder -name '*bat.c' -o -name 'bat*.c' 

Matching Only Files

To find only regular files—and not folders, symbolic links, and special device nodes—you can use -type f . This is frequently recommended and sometimes quite appropriate. but often not what you really want, especially if you’re running find for the purpose of examining the output yourself. If you had a symbolic link that matched your search, wouldn’t you want to know about it?

If you want to find both regular files and symbolic links, you can use:

find /path/to/folder -name '*bat*' \( -type f -o -type l \) 

That uses the -o operator and also parentheses for grouping (which must be quoted so the shell does not treat them specially; otherwise you’ll get a syntax error).

But suppose you only want to see symbolic links that ultimately point to a regular file (and not symbolic links to directories, device nodes, etc.). That’s actually even simpler: use -xtype instead of -type . Provided you’re not running find with -L flag, -xtype on a symbolic link tests the type of the file the link points to.

find /path/to/folder -name '*bat*' -xtype f 

If you have a symlink to another symlink to a file, -xtype f will match it even though its direct target is another symlink rather than a regular file. This is almost always what you want.

Often people think they want -type f , but really they want -xtype f .

Getting Detailed Output

find ‘s default action if you don’t specify one is -print . All the commands given above are equivalent to themselves with -print tacked on at the end.

find is often used to run commands based on the files found—often, commands that make changes. But there are also other actions whose purpose is to display results, besides -print . Of particular interest is -ls :

find /path/to/folder -name '*bat*' -ls 

This gives detailed information on each file, in a multi-column format, similar to (though not quite the same as) what you would see by running ls file .

Further Reading

For more information on find and other ways to find files, see:

  • The find manual page, accessible online or by running man find in a terminal.
  • The GNU findutils reference manual, providing extensive documentation on the find , locate , and xargs utilities.
  • FindingFiles in the Ubuntu help wiki, which shows how to use find as well as several other methods.

Источник

Оцените статью
Adblock
detector