Summary of linux command techniques that I knew when I was a fledgling engineer

Introduction

When I first entered the scene, I was struck by the black screen of the terminal, but now it's the longest time I've been watching.

It's been a year and a half since I started programming, and I've put together a list of Linux-based command techniques that I use almost every day!

If this is suppressed, fledgling engineers should be able to survive even if they are thrown into the field for the time being!

Command summary

ls Abbreviation for list. It will display the files and folders under the directory where you typed the command.

Example of using ls

> ls

pwd Abbreviation for print working directory. It will show you the location of the current directory.

Example of using pwd

> pwd

→ /Users/hoge/path

cd Abbreviation for change directory. You can move the directory. You can move to your home directory by typing cd without adding anything, and you can move to the previous directory by typing cd -.

cd usage example

> cd path/to/hoge
→ Move to hoge directory

mv Abbreviation for move. You can move files and directories. I also use this when I want to rename a file.

Example of using mv

Move files. hoge.txt some_Move to directory
> mv hoge.txt some_directory

Rename the file. hoge.txt to fuga.Rename to txt
> mv hoge.txt fuga.txt

cp Abbreviation for copy. cp 1st argument 2nd argument copies the file of 1st argument to 2nd argument.

Example of using cp

hoge.txt some_fuga in directory.Copy with the name txt
> cp hoge.txt some_directory/fuga.txt

find As the name implies, it is a command to find files and directories. It's a little complicated and difficult, but it's very convenient when you get used to it.

Use case of find

The extension is`md`Find the file under the current directory
(If you execute it in the root directory, all directories will be searched, so the search results will be ugly.)

> find . -name "*.md"

grep Abbreviation for Global regular expression print. It is a command that reads a file from the directory under the command and searches for text.

Example of using grep

> grep hoge

→
hoge.md
17:hogehoge/fugafuga

fuga.md
25:→ /Users/hoge/path
33:> cd path/to/hoge
34:→ Move to hoge directory

hogefuga.md
27:/Users/hoge

By the way, there is an evolved version (variant?) Of grep called ripgrep, and ripgrep is overwhelmingly faster, so I recommend this.

ripgrep

cat Abbreviation for concatenate. I'm not a cat. You can browse the contents of the specified file.

Example of using cat

> cat hoge.txt

→
hogehogehogeho
hogehogehogeho
hogehogehogeho

more You can browse the contents of the specified file. Unlike cat, the contents of the file are displayed little by little, so it is convenient to use when there are many lines in the file. After typing more, press q to return to the original screen.

more usage example

> more hoge.txt

less It's a command like an evolved version of more. This time I introduced both more and less, but basically it's okay if you can use only one of them!

If you hit the command like more, it will switch to browse mode and press q to return to the original screen.

The article below is also helpful, so be sure to check it out.

11 less command tips that engineers should know

Example of using less

> less hoge.txt

head Only the ○ line from the top of the specified file is displayed. If you specify head -10 and the number, you can do something like ** 10 lines ** from the top of the file.

Example of using head

head -10 hoge.txt

→
(hoge.The top 10 lines of txt are displayed)

tail The opposite of head, the specified file is displayed only in the ○ line from the bottom. As with head, if you specify tail -10 and the number, you can display only ** 10 lines ** from the bottom of the file.

Example of using tail

tail -10 fuga.txt

→
(fuga.The bottom 10 lines of txt are displayed)

echo When an argument is applied, the contents of that argument are output. It's a command you often see when building an environment.

Example of using echo

> echo $PATH

→
/Users/sukebeeeeei/.tfenv/bin:/Users/sukebeeeeei/node_modules:/Users/sukebeeeeei/.nodenv/bin:/Users/sukebeeeeei/.nodenv/shims:/usr/local/opt/[email protected]/bin:/Users/sukebeeeeei/go/bin:/Users/sukebeeeeei/.goenv/shims:/Users/sukebeeeeei/.goenv/bin:/Users/sukebeeeeei/.pyenv/shims:/Users/sukebeeeeei/.pyenv/bin:/Users/sukebeeeeei/.rbenv/shims:/Users/sukebeeeeei/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/opt/X11/bin:/usr/local/texlive/2018/bin/x86_64-darwin/:/Users/sukebeeeeei/dotfiles

(PATH is displayed)

touch Updates the file's time stamp to the current time. If the file does not exist, create an empty file. Why is it named touch? Please let me know.

use example of touch

# hoge.If you have txt
> touch hoge.txt

→
(File time stamp is updated)

# hoge.If there is no txt
> touch hoge.txt

→
(Hoge with empty contents.txt will be created)

vi ・ vim

You can open vi ・ vim. It is used when it is troublesome to open an editor such as ** vscode or atom **.

After that, if you do vim hoge.txt, it will generate a file if hoge.txt does not exist, so

--Create a file --Slightly write to that file

In that case, it is a frequent command. As for vi and vim, all engineers should master the basic commands, so if you have never used it, please practice it at this time.

Vim course starting from knowledge 0

Example of using vi / vim

> vim hoge.txt

mkdir Abbreviation for ** make directory **. As the name implies, it is a command to create a directory.

Example of using mkdir

> mkdir hoge

→
(hoge directory is created)

rm Abbreviation for remove. This command is used to delete a file. If you make a mistake in using it, it will be ** ruined **, so please use it systematically.

rm ~ Delete directories and files

Example of using rm

> rm hoge.txt

→
hoge.txt will be deleted

rmdir Abbreviation for remove directory. This command is used to delete a directory. You can only delete directories that are empty. So I rarely use it w (as introduced in this article)

If you want to erase the directory in which the file is located, you will most likely use > rm -rf hoge_directory.

Example of using rmdir

> rmdir hoge_empty_directory

→
(hoge_empty_Delete directory)

ln Abbreviation for link. You can create symbolic links. What is a symbolic link? Please refer to the following article for those who say.

How to set a symbolic link

Example of using ln

> ln -s ~/dotfiles/vimrc/_vimrc ~/.vimrc

man Abbreviation for manual. A command (confusing) that displays a command description. ** "Ahh, how do you use this command?" ** or ** "What are the options for this command?" **.

Example of using man

> man ls

→
(The manual for the ls command appears)

sudo Abbreviation for superuser do. I often read it as ** Sudu ** or ** Mr. Sudo **.

Use this when you want to execute commands with administrator privileges. If you add sudo, you will be asked for the password after hitting the command, so enter it to execute the command.

Example of using sudo

> sudo vim hoge.txt

→Password:
(You can execute the command by entering the password and pressing Enter)

history As the name suggests, the history of commands you have typed so far is displayed.

Example of using history

> history

→
...
11879 less must_cover_linux_commands.md
11880 tail must_cover_linux_commands.md
11881 more must_cover_linux_commands.md
11882 head must_cover_linux_commands.md
11883 more must_cover_linux_commands.md -n 100
11884 more must_cover_linux_commands.md -n 3
...

Convenient technique collection

Here are some techniques that ** make great progress ** when you know them when working with the command line. It's a matter of course for those who know it, but not everyone knows it. I first learned about it a few months after I first entered the site, and when I first learned about it, I was very impressed.

“< > >>” I think it's easier to understand if you give an example rather than explain it, so I'll give you a reference example.

> ruby calculate.rb < input_data.txt
(calculate.Execute rb and input as the data_data.Specify txt)

> ruby calculate.rb > output_data.txt
(calculate.Execute rb and output the output result_data.Output to txt. output_data.If txt does not exist, the file will be created without permission. output_data.If txt exists, the contents of the file will be overwritten with the output result)

> ruby calculate.rb >> output_data.txt
(cdalculate.Execute rb and output the output result_data.Add it to the end of txt.> output_data.Unlike txt, the point is that the contents of the file are not overwritten)

> ruby calculate.rb < input_data.txt > output_data.txt
(<When>,and>>は組み合わせるこWhenができます。↑では、calculate.Execute rb and input the data used for execution_data.Output the contents of txt and the execution result_data.Output to txt)

pipe (|) You can connect commands to each other. ** I use it a lot. ** **

> cat hoge.txt | grep fuga
(hoge.Find the location in txt that contains the string fuga)

(), `` It executes the command inside the parentheses of ` ()` and returns the result. However, I don't think this alone makes any sense, so I'll give you an example.

> cat $(ls)

(First$(ls)Is executed and the result is the argument of cat. As a result, all the contents of the files under the directory where the ↑ command is executed are displayed. )

Regular expressions

For example, the grep command allows you to use regular expressions such as*,+, and .. Rather, grep is originally an abbreviation for Global regular expression print, so it's natural that regular expressions can be used.

* and + are repetitions of the same character (* contains an empty string, whereas + is one or more characters). . Is an arbitrary character, isn't it?

> grep .*hoge

(Search for sentences containing hoge)

Try to combine commands and techniques

Now, let's combine the commands and techniques introduced so far!

Wow, when was that command?

You can search past commands with ctrl + r, but you can do the same with the ↓ command.

> history | grep command_name

(For mac) I want to know the name of the latest ○ files in Download

> ls -lat ~/Downloads | head -10
total 3408200
drwxr-xr-x+ 112 keisuke  staff       3584  1 18 21:49 ..
drwx------@ 569 keisuke  staff      18208  1 18 18:54 .
-rw-r--r--@   1 keisuke  staff      65540  1 18 18:54 .DS_Store
-rw-r--r--@   1 keisuke  staff     254894  1 18 18:54 finished.zip
-rw-r--r--@   1 keisuke  staff       1692  1 17 22:53 dupSSHkey.pem
-rw-r--r--@   1 keisuke  staff     128909  1 13 10:19 assignment-2-problem.zip
-rw-r--r--@   1 keisuke  staff     129247  1 12 11:43 01-start.zip
-rw-r--r--@   1 keisuke  staff      26651  1 12 11:43 learning-card.pdf
-rw-r--r--@   1 keisuke  staff     236506  1 12 11:25 forms-03-finished.zip

When you want to see the log

tail -f log/development.log

This command is used when you want to see the log. When I'm writing Rails, I usually hit this command to allocate one panel of terminals for checking logs.

When you want to detect only a specific character string in the log

tail -f log/development.log | grep "The character string you want to search"

Or

cat log/development.log | grep "The character string you want to search"

I use this command when I want to do print debugging.

I want to insert a string in .zshrc (or .bashrc)

It's a command you often see when building an environment. The command is to output the contents of'' with echo and insert the output result into ~ / .zshrc.

echo 'export PATH=">HOME/.nodenv/bin:>PATH"' >> ~/.zshrc

I want to find out a specific process ID

This command checks if a process called grep is running.

> ps aux | grep grep
sukebeeeeei          80328   0.0  0.0  4268280    656 s012  S+    9:52PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn grep

When you want to list the files in the directory two directories below

Suppose you have a directory with a structure like ↓ (the directory where the articles to be uploaded to my Qiita are saved)
>  qiita-outputs git:(master) ✗ tree
.
├── README.md
├── alfred_techniques.md
├── articles
│   └── nukistagram_release.md
├── english_acronym.md
├── env_and_env_fetch
│   └── sample.rb
├── favorite_english_site.md
├── must_cover_linux_commands.md
├── must_gem_list.md
├── path_understanding.md
├── peco_intro.md
├── really_good_software.md
└── software_engineer_general_knowlegde.md

2 directories, 12 files

(Try using regular expressions. You can see the structure of the directory in multiple layers)
>  qiita-outputs git:(master) ✗ ls *
README.md                              must_gem_list.md
alfred_techniques.md                   path_understanding.md
english_acronym.md                     peco_intro.md
favorite_english_site.md               really_good_software.md
must_cover_linux_commands.md           software_engineer_general_knowlegde.md

articles:
nukistagram_release.md

env_and_env_fetch:
sample.rb

(Check the files in the two directories below)
>  qiita-outputs git:(master) ✗ ls */*
articles/nukistagram_release.md env_and_env_fetch/sample.rb

I want to see only the first 10 lines of the output result

> ls */* | head -10

Since the output result will be long, I would like to check a part of it

> ls */* | less

When you want to erase all docker images

> docker rmi $(docker images -q)

Or

> docker rmi `docker images -q`

You can see a list of docker images in docker images. By adding -q as an option, only the image ID will be displayed.

Process it first with $ (docker images -q) and run docker rmi on the return value. (Rmi: remove image)

For your reference. If you want to see only the first 5 of docker images.

> ~ docker images | head -5
REPOSITORY                                      TAG                          IMAGE ID            CREATED             SIZE
<none>                                          <none>                       8769696a985d        4 days ago          910MB
<none>                                          <none>                       694f9395f0e7        4 days ago          1.04GB
<none>                                          <none>                       8ff2255c3c50        4 days ago          995MB
<none>                                          <none>                       a1457d2c753d        2 weeks ago         995MB
<none>                                          <none>                       788141cacfc5        2 weeks ago         998MB

When you want to see only the first 5 image IDs of docker images

>  ~ docker images -q | head -5
8769696a985d
694f9395f0e7
8ff2255c3c50
a1457d2c753d
788141cacfc5

Output grep search results to a file

> grep hoge > hoge_search_result.txt

I want to replace the output text and display it

The command sed (short for stream editor) allows you to replace text and do a lot of technical and interesting things.

> echo abcabc | sed s/ca/12/g

→ ab12bc

When you want to know the contents of the lines before and after the match with grep

grep -You can also display n lines before and after the match with C n

> grep -C 3 "search_word" search_file

When you want to know the contents of options of a certain command

For example, suppose you want to know how to handle regular expressions with the find command, and whether you should add options. In such a case, this.

Display a manual about the find command, and in that manual"regular expression"Display the part that matches with, including the 3 lines before and after it

> man find | grep -C 3 "regular expression"

     The options are as follows:

     -E      Interpret regular expressions followed by -regex and -iregex pri-
             maries as extended (modern) regular expressions rather than basic
             regular expressions (BRE's).  The re_format(7) manual page fully
             describes both formats.

     -H      Cause the file information and file type (see stat(2)) returned
--
--
     -regex pattern
             True if the whole path of the file matches pattern using regular
             expression.  To match a file named ``./foo/xyzzy'', you can use
             the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'', but not
             ``xyzzy'' or ``/foo/''.

     -samefile name

Finally

The commands and techniques introduced above are also ** more powerful when combined with shell scripts and aliases, but I gave up because they deviated from this article **. If this article buzzes, I'd like to write an article that explains the area.

If you have any useful tips like "Wai uses these commands!", Please leave a comment! Please report any mistakes or typographical errors!

Promotion

I'm buzzing so I'll put an advertisement suddenly w This is an article I wrote with a lot of energy. It's a self-confident work, so please read it!

[Learning English] Introducing 5 interesting and studying programming YouTubers from overseas and other recommended videos from English-speaking countries!

[Work efficiency] Summary of Mac OS software that I personally use even if I charge for it because it is too excellent [5 selections]

The story that terminal operation was dramatically streamlined using peco

References

Linux commands become familiar when you understand the meaning of words

New Linux textbook

[[Try and understand] How Linux works-Basic knowledge of OS and hardware learned through experiments and illustrations](https://www.amazon.co.jp/gp/product/477419607X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag= affiliate3203-22 & creative = 1211 & linkCode = as2 & creativeASIN = 477419607X & linkId = cd8945dcad25f265404e69f7ef8f0efb)

The idea of UNIX-its design concept and philosophy

bonus

I made my own blog with Gatsby.js. I will post various articles! http://keisukee.com/

Recommended Posts

Summary of linux command techniques that I knew when I was a fledgling engineer
A brief summary of Linux
A note I was addicted to when making a beep on Linux
A tool that seems to grow explosively when a fledgling engineer knows [Mixed]
Summary of petit techniques for Linux commands
Log when I was worried that I could not connect to Wi-Fi on Linux
[Linux] A list of unique command selections that are convenient but unexpectedly unknown
[Linux] Let's talk about when I stumbled upon a symbolic link I was using.
A story that I was addicted to when I made SFTP communication with python
Linux Ubuntu16.04 I got a little scary error when I ran a command using sudo
Machine learning memo of a fledgling engineer Part 1
Machine learning memo of a fledgling engineer Part 2
[Linux command] A memorandum of frequently used commands
Linux Command Summary
A story that was convenient when I tried using the python ip address module
The story of Linux that I want to teach myself half a year ago
Convenient Linux keyboard operation that I want to teach myself when I was in school
[Linux] A list of Linux commands that beginners should know
Summary of Linux (UNIX) commands that appeared in Progate
A brief summary of Linux antivirus software for individuals
A summary of what I have touched like a blog
A story that I was addicted to at np.where
Summary of things that were convenient when using pandas
Summary of points to keep in mind when writing a program that runs on Python 2.5
What I was addicted to when dealing with huge files in a Linux 32bit environment
Memo (March 2020) that I was addicted to when installing Arch Linux on MacBook Air 11'Early 2015
[Linux] Basic command summary
A story that stumbled when I made a chatbot with Transformer
A general-purpose program that formats Linux command strings in python
A rough summary of the differences between Windows and Linux
[Python3] List of sites that I referred to when I started Python
The goodness of the touch screen disappeared when the tablet PC was made into a Manjaro i3 environment