قالب وردپرس درنا توس
Home / Tips and Tricks / How to use the ex command on Linux

How to use the ex command on Linux



A terminal window on a Linux computer with an Ubuntu-style desktop.
Fatmawati Achmad Zaenuri / Shutterstock

On Linux, fd is an easier alternative to find command. It has a simplified syntax, uses sensible default values ​​and has built-in common sense behavior. Let̵

7;s take it through its steps.

ex versus find: What is the difference?

The fd the command is not intended to replace the traditional find command, which has been on Linux, yes, forever. instead, fd trying to satisfy most common uses of find in a clearer way – and it is often eight or nine times faster than find. You can see some of its benchmarks on the project’s GitHub archive page.

fd has a colored output, similar to that of some ls positions. It is recursive, but does not search for hidden directories by default. It knows about Git and also automatically ignores all patterns in your “.gitignore” file.

fd capital letters are sensitive by default. But if your search pattern contains an uppercase letter, fd operates in a case sensitive mode. Of course, you can override the default values, but in many cases they work to your advantage.

RELATED: How to use all Linux search commands

Install ex

Since Ubuntu 19.04 (Disco Dingo,) you can install fd directly by calling the officially maintained package with apt-get. If you are running an older version of Ubuntu, check the installation instructions on the Git hub page.

Enter the following:

sudo apt-get install fd-find

sudo apt-get install fd-find in a terminal window.

In Ubuntu, the command is fdfind to avoid a collision with another existing tool. If you want it to be fd, you can set an alias:

alias fd=fdfind

alias fd = fdfind in a terminal window.

To make the alias persistent so that it remains available after reboot, put it in your “.bashrc” or “.bash_aliases” file.

RELATED: How to create aliases and scale functions on Linux

To install fd Write Fedora on Fedora:

sudo dfn install fd-find

sudo dnf install fd-find in a terminal window.

Write the following in Manjaro:

sudo pacman -Syu fd

sudo pacman -Syu ex in a terminal window.

fd versus fdfind

To avoid confusion, we have left the command with its default name, fdfind, on our Ubuntu test computer. fd and fdfind is exactly the same command, as you see in the following example (if you ask fdfind to show its version it is called “ex”):

fdfind --version

fdfind --version in a terminal window.

We will call the command “fed”, but in the examples we use Ubuntu “fdfind.” On other Linux distributions, you can type “fd” instead of “fdfind” to save a few keystrokes.

Simple searches with ex

If you use fd without command line options, it behaves a bit like ls, except that it lists files in subdirectories by default.

Enter the following:

fdfind

fdfind in a terminal window.

The output is displayed in different colors for different file types and directories.

Output from fdfind in a terminal window.

To view files of a certain type, use -e (supplement) option. Note that you do not need to precede the extension by a period (.) Nor is it case sensitive.

For example, you can type the following:

fdfind -e png

fdfind -e png in a terminal window.

Now the only listed files are PNG image files.

output from fdfind -e png in a terminal window.

If you want to search for a single file, type the name of the command line, then:

fdfind index.page

fdfind index.page in a terminal window.

The file is found and happens to be in a subdirectory. We did not have to tell fd to search recursively.

If you want to start the search in a specific directory, include a file path on the command line. The following command starts a search in the “/ etc” directory and searches for files that contain “passwd” in the file name:

fdfind passwd /etc

fdfind passwd / etc in a terminal window.

Here we search for all C source code files that contain “coordination” in the file name:

fdfind -e c coord

fdfind -ec code in a terminal window.

Two matching files were found.

ex and Git

Git is an extremely popular control system for source code versions. If you use Git on your computer, you are probably using “.gitignore” files to tell Git which files it should handle and which it can ignore. By default is fd respects the settings in your “.gitignore” files.

In this directory we have a Git archive and “.gitignore” file. We write the following:

ls -adl .git*

ls -adl .git * in a terminal window.

Let’s ask fd to list all files that contain “coordination” in the file name. We then repeat the search and use -I (no ignore) options. This tells fd to ignore the settings in the “.gitignore” file and report each matching file.

To do all this we write the following:

fdfind coord
fdfind coord -I

fdfind code in a terminal window.

The two extra files in the second set of results are object files. These are created when a file program is compiled. They are then used by the link to create the final executable version of the program.

Object files are usually ignored by source version control programs. They are regenerated every time you compile your program, so you do not need to store copies of them. There is an entry in the “.gitignore” file that instructs Git to ignore object files and by default fd ignore them too.

The -I (no ignore) alternative forces fd to return everything it finds, rather than being guided by the .gitginore file.

File types and case sensitivity

You can ask fd to search for directories, files (including those that are executable and empty) and symbolic links. You can do that by using -t (type) option, followed by one of the letters below:

  • f: Fil.
  • d: Catalog.
  • l: Symbolic link.
  • x: Executable file.
  • e: Tom fil.

The following is looking for a directory called images:

fdfind -td images

fdfind -td images in a terminal window.

A match is found, a subdirectory that is lower than the current one.

Let’s see how case sensitivity works with search patterns. We write the following to first search for files that contain “geo” in their filenames, and then for those that contain “Geo” in their filenames:

fdfind -tf geo
fdfind -tf Geo

fdfind -tf geo in a terminal window.

In the first command, we used a small search pattern that caused fd to work in a case-insensitive way. This means that both “Geo” and “geo” are valid matches.

Our second command contained a large character, which caused fd to function in a case-sensitive way. This means that only “Geo” is a valid match.

command Execution

The fd command lets you launch another command and run it on each of the found files.

Let’s say we know there’s a zip file somewhere in our source directory directory tree. We can look for it with the following command, which searches for files with the ZIP extension:

fdfinf -e zip

fdfinf -e zip in a terminal window.

With -x (exec) option, you can transfer each found file to another command to be processed by it. For example, we can type the following to call the unzip tool to extract our ZIP file (“{}” is a placeholder that represents the found file):

fdfind-e zip -x unzip {}

This will unpack the file in the current work directory. If we want it to be unpacked in the directory containing the ZIP file, we can use one of the following placeholders:

  • {}: The complete file path and the name of the found file.
  • {/}: The file name of the found file.
  • {//}: The directory that contains the found file.
  • {/.}: The filename of the found file without the extension.

In order for our ZIP file to be found and unpacked in the directory that contains it, we can use unzip -d (directory) option and send the placeholder for the parent directory ({//}):

fdfind-e zip -x unzip {} -d {//}

fdfind-e zip -x unzip {} -d {//} in a terminal window.

The ZIP file is then found and unpacked in its parent directory.

Output from fdfind-e zip -x unzip {} -d {//} in a terminal window.

Your Go-to Find?

Since it covers the most common uses with such simplicity, fd can easily become your “find” command. When you need its more advanced features, you can always return to the experienced veteran, find.




Source link