Beginners! Basic Linux commands and how to use them!

Hello everyone!

This is a repost of https://nokonoko1203.qrunch.io/entries/dSVhxUEcdi3WsU1V

I think that few people who come to see this page say "I don't know Linux", but I think that there are quite a few people who are familiar with Linux commands.

I don't know the whole picture, but a quick glance at this (Linux command list (man page list)) reveals that there are innumerable commands. I will.

It is not easy to grasp all of these and master them.

However, there are only a few commands that you actually use on a daily basis, and you don't have to remember them all!

This time, we will introduce carefully selected "minimum required commands"!

List of basic Linux commands

① Commands that must be used at a minimum

--cd: Change current directory --pwd: Show current directory --mkdir: Create directory --ls: Display a list of files and directories --rm: Delete files and directories --touch: If the file with the name passed in the argument does not exist, an empty file is created, and if it exists, the modification date and time of that file or directory is updated. --cp: Copy files and directories --mv: Move files and directories

② Useful commands if you can use them

--find: Find files and directories --nano: Easy-to-use beginner editor even for those who are not familiar with CUI --cat: Combine the contents of multiple files and output to standard output --less: Display the contents of the file while turning pages --grep: Search for a specific word in a file or standard input --chmod: Change file permissions --echo: The character string given by the first argument is output to the standard output as it is. --read: Substitute the character given by standard input into the variable with the name given by the first argument. --tmux: Operate multiple consoles with one console and keep the consoles in place --crontab: Set a schedule to automatically execute the program

I will explain these commands in two parts!

cd: Change current directory

cd is an abbreviation for Change Directory, which is a command to change the current directory (current directory).

Use the cd command to go to / Users / {Mac login name} / vagrant / ubuntu / workspace created in How to put Linux in Vagrant and connect with SSH to Mac Let's do it.

local:~ {Your username}$ cd ~/vagrant/ubuntu/workspace

local:workspace {Your username}$

I think the display of the local: ○○ part has changed. The current directory is now changed.

Let's check this with another command.

pwd: Show current directory

pwd is an abbreviation for Print Working Directory, which, as the name implies, displays the current directory.

Let's actually display the current directory.

$ pwd

/Users/{Your username}/vagrant/ubuntu/workspace

As you can see, the pwd command displays the absolute path.

One point glossary

Path: The location of the file or directory Absolute path: Path from the root (top level) directory Relative path: Path from the current directory

. Represents the current directory and .. represents the next higher directory.

Therefore, when you want to indicate the workspace directory when the current directory is ubuntu (/ Users / {your user name} / vagrant / ubuntu) in the previous example.

#Absolute path
/Users/{Your username}/vagrant/ubuntu/workspace

#Relative path
./workspace

Will be!

mkdir: create directory

mkdir is an abbreviation for make directory and you can create a directory.

Let's try creating a demo directory in the ʻubuntu` directory.

#Show current directory
$ pwd
/Users/{Your username}/vagrant/ubuntu/workspace

#Move to the next higher directory with the cd command
$ cd ..

#Check again
$ pwd

/Users/{Your username}/vagrant/ubuntu

#Create demo directory in ubuntu directory
$ mkdir demo

The demo directory is now created.

Let's check with the ls command!

ls: Show list of files and directories

ls is an abbreviation for List and displays files and directories in the current directory.

Let's use it immediately.

$ ls
demo		workspace

Will be displayed.

You can see that there are two directories in the ubuntu directory, the demo directory created earlier and the workspace directory that existed from the beginning!

By the way, you can add options to Linux commands.

There are many options, but you can see "hidden files (dot files)" by adding the option -a with the ls command.

Let's try it out!

#Home directory (tilde:~)
$ cd ~

#Various files and directories in the home directory are displayed
$ ls
Applications			Users
Desktop				home
Documents
・ ・ ・

# -With the a option, ".The files and directories marked with "" are displayed.
$ ls -a
.
..
.Trash
.bash_history			.ssh
.bash_profile			.bashrc
.config				.bundle

・ ・ ・

Applications			Users
Desktop				home
Documents

With the -a option like this, the files and directories marked with "." Are displayed.

Files that start with a dot are often software configuration files or temporary files, .bash_profile and .bashrc are environment variable settings, and .ssh contains information necessary for connecting ssh. Keep in mind that it's an important and frequent fix!

rm: Delete files and directories

rm is an abbreviation for remove, and you can remove files and directories with the command.

However, you need to add the -r option when deleting the directory.

$ pwd
/Users/{Your username}/vagrant/ubuntu

$ ls
demo		workspace

# -no r option
$ rm demo

#I get an error
rm: demo: is a directory

# -r option available
$ rm -r demo
$ ls
workspace

The demo directory should be deleted this way and only the workspace should be visible.

The -r option means" recursively "and is used when you want to erase everything including the contents of a directory, but you need to specify the option even for directories that do not contain files.

touch: If the file with the name passed in the argument does not exist, an empty file is created, and if it exists, the modification date and time of that file or directory is updated.

touch is an abbreviation for change file access and modification times, which is a command to change the last access date and time and last modification date and time of a file, but you can also create a new file by specifying a file name that does not exist.

This time, we will create a new file in the ubuntu directory.

#Check the current directory
$ pwd
/Users/{Your username}/vagrant/ubuntu

#Create tmp file
$ touch tmp
$ ls
tmp		workspace

You have now created a new file.

cp: Copy files and directories

cp is an abbreviation for copy You can copy files and directories. Specify the copy source file directory in the first argument and the copy destination file directory in the second argument.

Move the tmp file created above to the workspace directory!

#Specify the tmp file you want to copy with the first argument, the path you want to copy with the second argument, and the name of the newly created file(tmp)Specify
$ cp tmp ./workspace/tmp

#Move to workspace
$ cd ./workspace

#Check the contents of the directory
$ ls
tmp

It's a little troublesome to specify the path, but you can copy the file with this!

When copying a directory, you must specify the -r option to copy recursively.

mv: Move files and directories

mv stands for move and you can move files and directories!

Let's create a new file in the ubuntu directory and move it to workspace!

#Check the contents of the directory
$ ls
tmp		workspace

#Create tmp2 file
$ touch tmp2
$ ls
tmp		tmp2		workspace

#Move to workspace
$ mv tmp2 ./workspace
$ cd ./workspace
$ ls
tmp	tmp2

The mv command can also be used to rename.

$ ls
tmp	tmp2

#tmp2 file name demo_Change to file
$ mv tmp2 demo_file
$ ls
demo_file	tmp

You can move and modify files in this way.

Note that both can be done with the same command, but it's a very useful command!

So far, we have explained a total of eight commands.

These are the basics of the basic operations using the terminal, so let's try and remember them!

There are many other options besides the ones I've introduced, so check them out and give them a try!

Next time, I will introduce other commands that are useful if you can use them!

Recommended Posts

Beginners! Basic Linux commands and how to use them!
Basic knowledge of Linux and basic commands
How to install and use Tesseract-OCR
How to use .bash_profile and .bashrc
How to install and use Graphviz
How to use linux commands in Visual Studio Code terminal
[For beginners] How to use for statements on Linux (variables, etc.)
[Linux] How to subdivide files and folders
How to install and use pandas_datareader [Python]
[Linux] How to use the echo command
How to use the Linux grep command
How to set up and use OMC Log Analytics --Linux version -
Basic LINUX commands
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use Jupyter notebook [Super Basic]
How to use is and == in Python
How to use pandas Timestamp and date_range
How to install fabric and basic usage
How to use lists, tuples, dictionaries, and sets
How to use data analysis tools for beginners
How to handle Linux commands well from Python
How to switch between Linux and Mac shells
[Python] How to use hash function and tuple.
How to install Cascade detector and how to use it
[wxpython] How to use wx.lib.plot basic & time axis
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
Linux commands to remember
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[Python] [Django] How to use ChoiceField and how to add options