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

What is the use of command aliases in linux

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what is the use of command aliases in linux", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of command aliases in linux?"

An alias is a shortcut

One of the most wonderful things about Linux shell is that you can use tens of thousands of options and concatenate commands to perform truly complex operations. Well, maybe this beauty is in the eyes of the bystander, but we think this function is very practical.

The downside is that you often need to remember combinations of commands that are difficult to remember or difficult to type. For example, the space on the hard drive is very valuable, and you want to do some cleaning work. Your step may be to find something hidden in your home catalog. One of the criteria you can use to judge is to find content that is no longer in use. Ls can help you:

Ls-lct

The above command shows the details of each file and directory (- l) and the time each item was last accessed (- c), and then it sorts the list (- t) in the order from the most recent access to the least access.

Is that hard to remember? You may not use the-c and-t options every day, so maybe. Anyway, define an alias, such as:

Alias lt='ls-lct'

It would be easier.

Then, you may want the list to show the oldest files first:

Alias lo='lt-F | tac'

Figure 1: using lt and lo aliases.

Here are some interesting things. First, we use an alias (lt) to create another alias-- that's fine. Second, we pass a new parameter to lt (which in turn is passed to ls through the definition of the lt alias).

The-F option appends special symbols to the name of the project to better distinguish between regular files (without symbols) and executable files (with * appended), catalog files (ending with /), and all linked files, symbolic link files (ending with the @ symbol), and so on. The-F option is used when you go back to monochrome terminals and there is no other way to easily see the differences between list items. It is used here because when you pass the output from lt to tac, you will lose the color of ls.

The third thing we need to pay attention to is that we use pipes. Pipes are used when you pass the output of one command to another. The second command can use these outputs as its input. In many shell, including Bash, you can use the pipe character (|) to pass.

Here, you import the output from lt-F to tac. The command tac is a bit of a joke. You may have heard of the cat command, which is nominally used to connect files to each other (concat), while in practice, it is used to print the contents of a file to a terminal. Tac does the same thing, but it outputs what it receives in reverse order. Do you get it? Cat and tac, technical people can be interesting sometimes.

Both cat and tac can output content passed through the pipeline, in this case, a list of files sorted in chronological order.

So, after a bit of digression, what we end up with is that this list lists the files and directories in the current directory in reverse order of freshness (that is, the old ones come first).

* you need to note that when running lt in the current directory or any directory:

# this can work: lt# this can also be: lt / some/other/directory

…… Lo only works in the current directory:

# this works: lo# and this is not: lo / some/other/directory

This is because Bash expands the components of the alias. When you type:

Lt / some/other/directory

What Bash actually runs is:

Ls-lct / some/other/directory

This is a valid Bash command.

And when you type:

Lo / some/other/directory

Bash attempted to run:

Ls-lct-F | tac / some/other/directory

This is not a valid command, mainly because / some/other/directory is a directory, and cat and tac cannot be used for directories.

More alias shortcuts

Alias lll='ls-R 'prints out the contents of the directory and goes deep into the subdirectory to print the contents of the subdirectory, as well as the subdirectories of the subdirectory, and so on. This is a way to view everything in a directory.

Mkdir='mkdir-pv' allows you to create directories under directories at once. In the basic form of mkdir, to create a directory that contains subdirectories, you must do this:

Mkdir newdirmkdir newdir/subdir

Or this:

Mkdir-p newdir/subdir

And with this alias, you will only need to do this:

Mkdir newdir/subdir

Your new mkdir will also tell you what you did when you created the subdirectory.

Aliases are also a kind of protection.

Another advantage of aliases is that they can be used as a protection against accidental deletion or overwriting of existing files. You may have heard the rumors of this new Linux user when they run as root:

Rm-rf /

The whole system explodes. The user who decides to enter the following command:

Rm-rf / some/directory/ *

They did a good job of killing all the contents of their home catalog. The space between the directory and * accidentally typed here can sometimes be easily ignored.

Both of these situations can be avoided by the alias rm='rm-i' alias. The-I option causes rm to ask users if they really want to do this, giving you a second chance before you do irreparable damage to your filesystem.

The same is true for cp, which can overwrite a file without giving you any hints. Create a similar alias cp='cp-i' to keep it safe.

The above is all the content of the article "what is the use of command aliases in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 0

*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