Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use bat commands in Linux

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to install and use bat commands in Linux". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to install and use bat commands in Linux.

Under the Linux operating system, we often use the cat command to connect multiple files and print them to standard output, synthesize several files into one target file, and add several files to the target file.

I recently found a similar command on GitHub called "Bat", which is an alternative to the cat command with some very cool features such as syntax highlighting, Git integration, and automatic paging.

1 、 bat

Install bat

Arch Linux

Bat is available from the default software source of the Arch Linux operating system and can be installed directly on any Arch-based system using the pacman command.

Pacman-S bat

Fedora

Dnf install bat

MacOS

Brew install bat

Ubuntu 21.10

I download the .deb file from the release page of bat through the bat installed in the Ubuntu environment, which can be downloaded and installed by executing the following command.

Sudo apt-get install wgetwget https://github.com/sharkdp/bat/releases/tag/v0.18.3/bat_0.18.3_amd64.debsudo dpkg-I bat_0.18.3_amd64.deb

For other systems, you may need to compile and install from a software source, or refer to other operating system installation methods of bat on GitHub.

Use of the bat command

The use of the bat command is very similar to that of the cat command.

Use the bat command to create a new file:

Bat > jiaoben1.sh

Use the bat command to view the contents of the file:

Bat jiaoben1.sh

Use the bat command to view multiple files at the same time:

Bat jiaoben1.sh jiaoben2.sh

Use the bat command to merge the contents of multiple files into a single file:

Bat jiaoben1.sh jiaoben2.sh > test.txt

The above describes how bat creates new files, looks at the contents of files, and merges the contents of multiple files into a single file.

Next, let's take a look at some very cool features of the bat command.

The bat command supports syntax highlighting in most programming and markup languages, so let's use the cat and bat commands to display the contents of jiaoben1.sh to make a sharp contrast.

As you can see, the cat command displays the contents of the file in plain text format, while the bat command shows syntax highlighting and neat text-aligned format.

From the output of the above bat command, you can see that there are table lines. If you only want to display the line number (but not the table), you can use the-n parameter to achieve it. Although the cat execution will also display the line number, it is still not as good as the bat output.

Bat-n jiaoben2.sh

The bat command supports Git integration to easily view and edit files in the Git repository, and bat connects to Git to display changes to the index.

It is also worth noting that bat supports automatic paging.

Support for paging means that when the output of the file exceeds the screen display, the bat command will automatically transmit its output to the less command, you will be able to use the enter key to view the output page by page, and use the space bar to jump directly to the last line at the end of the text.

These are unnecessary when you use the bat command, and the bat command will automatically transfer the output of files across multiple pages.

Bat jiaoben3.sh

Compared with the cat command, when you view the contents of a jiaoben3.sh file across multiple pages, the prompt will jump directly to the last page of the file, and you will not be able to see the contents that span the beginning and middle of the multiple page files.

Cat jiaoben3.sh

If you want to achieve the effect of the bat command through cat, you need to use the output of cat to transfer to the less command so that you can view the content page by page from the beginning.

Cat jiaoben3.sh | less

Similarly, you can use the enter key to view the output page by page and use the spacebar to jump directly to the last line at the end of the text.

Bat customized theme

At present, there are 24 customized themes for bat, including: Coldark-Cold, GitHub, Monokai Extended Light, OneHalfDark, Solarized (light), TwoDark, Visual Studio Dark+, base16, zenburn, etc.

If you don't like the default theme, you can also modify it according to your own style, just execute the command: bat-- list-themes, and you can display the available themes.

# bat-list-themesTheme: 1337 / / Output the square of a number. Fn print_square (num: F64) {let result = f64::powf (num, 2.0); println! ("The square of {: .2} is {: .2}.", num, result);}.

For example, if you want to select a Dracula theme, you can directly execute: bat-- theme=Dracula file name

If you want to permanently change a theme, use export BAT_THEME= "Dracula" in your shell startup file to make the change permanent.

You can use the-- style parameter option to control the appearance of the bat output. Configuration elements include: line numbers, file headers, grid borders, Git modifications to display things other than the contents of the file, and the parameters are a comma-separated list.

-- style=numbers,changes can set the BAT_STYLE environment variable to make these changes permanent by displaying only Git modifications and line numbers instead of grid and file headers.

More bat can be used by executing the command man bat or bat-- help.

In addition, you can also view more details through the GitHub library of the bat project. I won't want to use cat again after I've used bat!

2 、 duf

In the Linux environment, we know that the df command is used to display the disk space available on the disk partition, and the default display unit is KB. You can also use this command to get information such as how much space the hard disk is occupied and how much space is left.

# dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda3 18439940 4943264 12559972 29% / tmpfs 506176 76 506100% / dev/shm/dev/sda1 99150 30003 64027 32% / boot/dev/sr0 4363088 4363088 0 100% / media/CentOS_6.5_Final

In addition, the du command also looks at the usage space, but unlike the df command, the Linux du command looks at the space used by the file and directory disk, which is somewhat different from the df command.

# du4. / .abrt8. /. Dbus/session-bus12. / .dbus4. / .nautilus40. /. Local/share/gvfs-metadata24. / .local / share/applications68. / .local / share72. / .local

Next, I will introduce you to a terminal tool, which you can understand as the traditional integrated version of Linux command df and du-duf.

Duf is a cross-platform disk usage tool written in Golang that allows you to easily check available disk space, categorize output, and render it in a user-friendly manner for Linux, Windows, macOS, Android, FreeBSD

Ubuntu installation method:

$sudo apt install gdebi# if you report an error, Failed to fetch http://security.ubuntu.com......# sudo apt-get update$ sudo gdebi duf_0.5.0_linux_amd64.deb# should download duf_0.5.0_linux_amd64.deb at https://github.com/muesli/duf/releases/download/v0.5.0/duf_0.5.0_linux_amd64.deb first.

MacOS installation method:

# install Homebrew first and follow the prompts to install it. / bin/zsh-c "$(curl-fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"# install dufbrew install duf# or sudo port selfupdate & & sudo port install duf

Windows installation method:

Choco install duf# or scoop install duf

After installation, you can use duf directly, and the method is also very simple.

Without adding any parameters, the details of all local devices, any mounted cloud storage devices, and any other special devices (including temporary storage locations, etc.) will be output

$duf

If you only want to see the details of the locally connected device, but not the others, you can do this:

$duf-only local

If you only want to sort the output in a specific order according to size, you can do this:

$duf-sort size

More parameters are used:

Duf / home / some/file according to the parameter, duf will only list specific devices and mount points duf-- all lists all content duf-- hide-network hides the network file system The corresponding-- hide-fuse-- hide-special-- hide-loops-- hide-bindsduf-- inodes lists the format of inodesduf-- output mountpoint,size,usage specified output and (mountpoint,size, used, avail, usage, inodes, inodes_used, inodes_avail, inodes_usage, type, filesystem) duf-- json outputs duf-- theme light in json format if duf cannot correctly detect the terminal's color. You can set a theme duf-- help to view all available commands for duf

3 、 tldr

For beginners learning Linux, the most problem is that there are endless commands in the Linux operating system, as well as various parameters of the use of method, in fact, you do not have to memorize it at all, when you use it to find relevant information or through the man command to view relevant documents, man manual output information is more, and for more parameters of the command, look through the man manual is also very difficult.

This time I would like to introduce you to a tool: tldr, its full name is: Too Long, Don't Read, Chinese meaning: too long do not look.

In fact, the result of the man command is basically too long to look. Tldr is a tool that you can understand as simplifying the help documentation of the man command and listing some of the most commonly used command usage of the Linux command, which is very easy to understand.

At the same time, tldr includes clients written in many languages, such as C, Go, iOS, Python, nodejs and so on. You can install them with different clients.

My environment here is Ubuntu. You need to install Node.js first, and then follow the following installation method:

Sudo apt-get install nodejssudo apt-get install npmsudo npm install-g tldr

After executing the above three commands, you need to execute tldr-- update, update the tldr to the latest status, and then you can use it directly.

Tldr-update

Use of tldr:

The use of the tldr command is also very simple, as long as tldr + the command you want to query

4 、 htop

The most commonly used tool for displaying process status information on the Linux operating system is the familiar top, which is a good helper for every system administrator.

Htop can be said to be an excellent substitute for top, which is written in C, is a cross-platform interactive process monitoring tool, has better visual effects, makes it easier to understand the current system at a glance, and allows you to scroll the list of processes vertically and horizontally to see their complete command line and related information, such as memory and CPU consumption. System-wide information is also displayed, such as average load or swap usage.

The information displayed can be configured through graphical settings, and can be sorted and filtered interactively, and process-related tasks, such as termination and reprocessing, can be completed without entering their PID.

Installation is also simple, as long as you execute the command: apt install htop.

Compared with top, htop has obvious advantages. In addition to its more perfect functions, it is also rich in color matching and more user-friendly as a whole.

5 、 glances

Glances is a cross-platform monitoring tool written in Python, which aims to present a large amount of system monitoring information through curses or Web-based interface. This information is dynamically adjusted according to the size of the user interface and is a top/htop substitute for GNU/Linux, BSD, Mac OS and Windows operating systems.

It can work in client / server mode, remote monitoring can be done through terminal, Web interface or API (XML-RPC and RESTful), and statistical data can be exported to files or external time / value databases.

In addition to listing all processes and their CPU and memory usage, it can also display other information about the system, such as:

Network and disk usage

Space and total space used by the file system

Data from different sensors, such as batteries

And a list of processes that have consumed too many resources recently

6 、 exa

When it comes to the ls command, we are no stranger to it. In the Linux environment, its main function is to list the files and subdirectories contained in the current directory. If there are too many files in the current directory, it is not good to use the command ls, because the output results are not consistent with the files you are looking for. First, secondary filtering is needed; second, when there are too many files, the terminal output results are slow.

EXA is a modern ls replacement for command-line programs shipped with Unix and Linux operating systems, giving it more functionality and better default values. It uses colors to distinguish between file types and metadata. It knows about symbolic links, extended attributes, and Git. It is small, fast, and has only one binary file.

How is EXA different from LS?

The terminal screen is no longer a bottleneck: exa is powerful, friendlier than ls, and outputs results faster

Color: exa uses a lot of colors

Do not conflict with ls command

Characteristics of EXA

Color: list of file types and how to distinguish files

Grid view: displays files in a grid of the same size as the terminal window

Long-term: display files and their metadata in tables

Tree view: displaying files and their children in the tree

Git integration: view the Git status of files while in the repository

Filter: hide files in the list, show invisible files, and sort output

Xattrs: lists the extended attributes of a file

Ubuntu 21.10 installation only needs to execute the command: apt install exa.

Display option

-1MALEMUTHUMALINE: one entry per line is displayed

-Gmam Musi grid: displays entries as a grid (default)

-lmmam Meltel long: show extension details and attributes

-Rmaine Mustel recurse: recursion to the directory

-Tremlin: recursive to the directory as a tree

-xmam Mushroom: sort the grid instead of sorting down

-Fmam Murmuri purify: displays type indicators by file name

-- colo [u] r: when to use terminal colors

-- Colo [u] r-scale: clearly highlight the level of file size

-- icons: display icon

-- no-icons: does not show icons (always overrides-- icons)

Filtering option

-amam Murall: show hidden and "point" files

-dmam MuthlistMuthdirs: list directories like ordinary files

-Lmam Meltel level = (depth): limit the depth of recursion

-rmaine Musi reverse: reverses the sort order

-smam Mermel sort = (field): which field is sorted by

-- group-directories-first: list directories before other files

-Dmam: only directories are listed.

-- git-ignore: ignore the files mentioned in. Gitignore

-I globs: global mode of files to be ignored (pipe delimited)

Pass the-- all option twice to display simultaneously. And... Catalogue.

7 、 fd

Fd, a program that looks for entries in the file system, is a simple, fast, and user-friendly alternative to the find command. Fd is not intended to replace all the functionality provided by the find command, but to provide reasonable default values in most use cases, which are useful in some cases.

Fd output is colored, similar to some ls schemas, it is recursive, and when searching for source code files in directories that contain Git repositories, fd automatically excludes hidden files and directories, including., .git directories, and ignores patterns in .gitignore files. In general, the search is faster and provides more relevant results on the first search.

By default, fd performs a case-insensitive pattern search in the current directory, but if your search pattern contains an uppercase letter, fd will perform it in case-sensitive mode. Of course, you can override the default values, but in many cases it is beneficial. Using find for the same search also requires you to provide additional command-line arguments.

Fd is available for many Linux distributions and can be installed in Ubuntu using standard repositories by executing the following command:

Sudo apt install fd-find

In Ubuntu operating systems, the command is fdfind to avoid conflicts with other existing utility names, and if you want it to be fd, you can set an alias:

Alias fd=fdfind

To leave the alias unchanged and still available after reboot, put it in the ".bashrc" or ".bash _ aliases" file.

Using fd with no parameter option is similar to ls, except that it also lists files in subdirectories by default.

Fdbat_0.18.3_amd64.debfd_8.3.0_amd64.debjiaoben1.shjiaoben2.shjiaoben3.shsnapsnap/firefoxsnap/firefox/631snap/firefox/731snap/firefox/commonsnap/firefox/currentsnap/snap-storesnap/snap-store/557snap/snap-store/558snap/snap-store/commonsnap/snap-store/currenttest.md

To search the current directory for all markdown files (.md or .MD), the find command is as follows:

Find. -iname "* .md". / test.md

Use fd to do the same search:

Fd * .mdtest.md

In some cases, fd requires additional options; for example, if you want to include hidden files and directories, you must use the option-H, which is not needed in find. For more command-line argument options for fd, see the fd GitHub repository.

8 、 ag

Ack and ag are two text search tools that are much easier to use than the native grep.

When searching for text in a specified directory, you do not need to specify various command-line options like grep, and the output also contains the file name and line number, and the search keyword is highlighted.

Ag can search the contents of files recursively faster than grep and ack.

Ubuntu 21.10 install ag and ackapt-get install silversearcher-agapt install ack

Common parameters of ag command

Ag-g (file name): similar to find. -name (file name)

Ag-I sed: ignore case search for text containing sed

Ag-A sed: searches for text with sed and displays n lines of text after matching content

Ag-B sed: searches for text with sed and displays n lines of text before matching content

Ag-C sed: search for text containing sed and display the matching content as well as the content of n lines of text before and after it

Ag-w sed: full match search, searching only the text that exactly matches what you are searching for

Ag-- java sed: search for text with sed in java files

Ag-- xml sed: search for text with sed in XML files

Use of the ag command

Search for the corresponding file based on the file name, using the-g parameter option.

Ag-g jiaobenjiaoben2.shjiaoben1.shjiaoben3.sh

Search for text about the sed keyword in the current directory.

Ag-w sedjiaoben1.sh10: sed-I '2d' "$new_filename"

For more parameter options, you can use them according to your actual scenarios.

9 、 axel

Axel is a command line multithreaded download tool, which can replace curl and wget when downloading files.

Ubuntu 21.10 install axel

Apt-get install axel

Axel parameter options

-n: specify the number of threads

-o: specify to save as a directory

-s: specify the maximum number of bits per second

-Q: silent mode

Case

# axel-n 10-o / tmp/ http://mirrors.163.com/ubuntu/ls-lR.gz# exa / tmp/ls-lR.gz/tmp/ls-lR.gz

As you can see, we can also download the ls-lR.gz file to the / tmp directory through axel.

10 、 pydf

Under the Linux system, we can use the df command to display information about the disk.

# df file system 1K-Block used used% mount point tmpfs 199180 1660 197520 / run/dev/sda3 19946096 10317864 8589692 55% / tmpfs 995884 0 995884% / dev/shmtmpfs 5120 4 5116 1% / run/lock/dev/sda2 524252 5344 518908% / Boot/efitmpfs 199176 100 199076 / run/user/1000/dev/sr0 3043440 3043440 0% / media/jacktian/Ubuntu 21.10 amd64tmpfs 199176 60 19911.1% / run/user/0

Ubuntu 21.10 install pydf

Apt install pydf

Pydf can be said to be an alternative to df, which shows the disk usage status in a more concise way.

At this point, I believe you have a deeper understanding of "how to install and use bat commands in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 238

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report