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!
It will display the files and folders under the directory where you typed the command.
print working directory.
It will show you the location of the current directory.
> pwd → /Users/hoge/path
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 path/to/hoge → Move to hoge directory
You can move files and directories. I also use this when I want to rename a file.
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 1st argument 2nd argument copies the file of
1st argument to
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.
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"
Global regular expression print.
It is a command that reads a file from the directory under the command and searches for text.
> 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.
concatenate. I'm not a cat.
You can browse the contents of the specified file.
> cat hoge.txt → hogehogehogeho hogehogehogeho hogehogehogeho
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 hoge.txt
It's a command like an evolved version of
more. This time I introduced both
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.
> less hoge.txt
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.
head -10 hoge.txt → (hoge.The top 10 lines of txt are displayed)
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.
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.
> 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)
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.
# 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)
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 hoge.txt
mkdir Abbreviation for ** make directory **. As the name implies, it is a command to create a directory.
> mkdir hoge → (hoge directory is created)
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 hoge.txt → hoge.txt will be deleted
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.
> rmdir hoge_empty_directory → （hoge_empty_Delete directory)
link. You can create symbolic links. What is a symbolic link? Please refer to the following article for those who say.
> ln -s ~/dotfiles/vimrc/_vimrc ~/.vimrc
A command (confusing) that displays a command description. ** "Ahh, how do you use this command?" ** or ** "What are the options for this command?" **.
> man ls → (The manual for the ls command appears)
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.
> 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.
> 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 ...
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)
> 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. )
For example, the
grep command allows you to use regular expressions such as
.. Rather, grep is originally an abbreviation for
Global regular expression print, so it's natural that regular expressions can be used.
+ 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)
Now, let's combine the commands and techniques introduced so far!
You can search past commands with ctrl + r, but you can do the same with the ↓ command.
> history | grep command_name
> 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
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.
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.
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
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
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
> ls */* | head -10
> ls */* | less
> 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 | 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 -q | head -5 8769696a985d 694f9395f0e7 8ff2255c3c50 a1457d2c753d 788141cacfc5
> grep hoge > hoge_search_result.txt
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
grep -You can also display n lines before and after the match with C n > grep -C 3 "search_word" search_file
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
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!
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!
[[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)
I made my own blog with Gatsby.js. I will post various articles! http://keisukee.com/