Shell in a nutshell - Basic Tips
Image source
Basic Command Line Syntax.
Today, I’m going to talk about basic shell commands. Since there is no icons to click, every action should be done by typing commands. In order to get used to Linux-like system, I will list up some of the simple shell commands for you. Only thing that you have yo do is typing those commands on the command line by yourself.
System controls
cd (change directory): This command might be the most frequently used than any other commands. You can move to any directory even hidden one.
# Move to current directory. (This does nothing but remember that "." means current directory.)
cd .
# Move to upper directory. (".." means upper directory.)
cd ..
cd ../../.. # (move to 3 upper directory.)
# Move to tmp directory. (Let's say we have "tmp" directory in a current directory.)
cd tmp # (Also "cd ./tmp" is possible)
cd ../../tmp # (tmp directory is located in 2 upper directories.)
# Move to home directory.
cd
# Move to the highest directory.
cd /
# Move to the previous directory.
cd -
pwd (print working directory): In the middle of moving directories, you might wonder which directory you are currently located in. Find current location by typing ‘pwd’ command.# Find the currently directory.
pwd
ls (list): It lists data files in the directory. “cd” after “ls” is always useful combinatory commands because you might wonder which files are located in the current directory.# List all the files in the current directory.
ls
# List all the files including hidden files. Dash command means it is optional. You can choose to use it or not.
ls -a
du (disk usage): It shows each file size. You can check the size of folders or files by this command.# Shows all the files' size in the current directory. Asterisk(*) means all things.
du -hs ./*
# You can choose a specific file such as "song.wav".
du -hs ./song.wav
df (disk filesystem): It shows the usage of the disk capacity. You can retrieve the information of total disk size, how much it is used, and how it is left.
# Show the information of disk capacity.
df -H
date : It displays date.# show date.
date
brew (in Mac), apt-get (in Linux) : It supports the users when they need to install some packages. This toolbox manages installation and uninstallation of packages by simple commands.# In Mac OSX, installation and uninstallation of python.
brew install python # installation
brew uninstall --force python # uninstallation
# In Linux, installation and uninstallation of python.
sudo apt-get install python # installation
sudo apt-get install remove python # uninstallation
sudo (superuser do) : sudo allows a user with proper permissions to execute a command as another user, such as the superuser. When you encountered permission errors while installing files, removing folders, or importing packages this command will let you do such jobs.# You can remove files right away. Be careful when you use this command, because it won't move it to trash bin but eliminate immediately. (recovery is impossible)
sudo rm song.wav
# sudo is useful when you install some packages. Many problems occurred during installation can be fixed by using this command.
sudo brew install python # brew is toolbox that helps you to install packages very easily.
shutdown : it shuts down the system.# Shut down the system immediately.
sudo shutdown -h now
# Restart the system.
sudo shutdown -r now
# You can also use "reboot" command for restarting the system.
sudo reboot
top : List all the ongoing processes.# List all the ongoing processes.
top
File controls
echo : Print messages on the monitor. Plus, Saving those messages as a text file is available.
# Print a sentence.
echo "Hello. My name is Hyungwon Yang."
# Save it as a text file.
echo "Hello. My name is Hyungwon Yang." > introduction.txt
# You can insert a line to break a sentence into two parts. "\n" means insert a line. In this case you have to give an option "-e" in front of the command because "-e" option interprets special characters such as "\n".
echo -e "Hello.\nMy name is Hyungwon Yang." > introduction.txt
cat (concatenate) : This command prints the materials of the files. By doing so, it allows manipulating texts such as deleting parts of sentences, attaching characters, combining more than two files, and replacing specific words to another. I will elaborate more usages next time.# print text file named "test.txt"
cat test.txt
# Combine multiple files. "test1.txt", "test2.txt", and "test3.txt" are combined into "new_test.txt" file.
cat test1.txt test2.txt test3.txt > new_test.txt
mkdir (make directory) : This generates directory.# Make a directory named "my_dir" in a current path.
mkdir ./my_dir # (You can also type as "mkdir my_dir")
# Make sub directories if it is not presented. If you want to make a "son" directory in a "father" directory but both of them does not exist, use "-p" option to make both directories at the same time.
mkdir -p ./father/son # It will make "father" directory first and then "son" directory inside of it.
rm (remove) : It removes files or directories. Deleting directories, however, needs “-r” option.# Remove "song.wav" and "test.txt" file.
rm song.wav test.txt
# Remove "my_dir" directory. Be careful, it will remove all the materials contained in the directory.
rm -r my_dir
# If you have any problems while removing files, then use "-f" force option.
rm -rf my_dir
mv (move) : This command moves files or directories to another directory.# Move "test.txt" file to "my_dir" directory. Suppose "test.txt" is in the current directory and "my_dir" is located inside of the "father" directory.
mv ./test.txt ./father/my_dir
# Move multiple files to "my_dir" directory.
mv {test1.txt,test2.txt,test3.txt} ./father/my_dir
cp (copy) : It copies files to another directory. You can copy the directory including all the subdirectories and files by using “-r” option.# Copy "test.txt" file to "my_dir" directory. Suppose "test.txt" is in the current directory and "my_dir" is located inside of the "father" directory.
cp ./test.txt ./father/my_dir
# Copy "tmp_dir" directory to "my_dir" directory (including all the files inside the directory).
cp -r tmp_dir ./father/my_dir
ln (link) : It generates twin directory and links both of them. For example, there is a “A” directory which contains 100 GB data and you want to copy this directory to 2 other directories. In this case, Duplicating “A” directory to those directories is undesirable because it wastes the disk capacity. To remedy this problem, you can make linking directories to 2 other directories in which contain just directional information that points to “A” directory. You don’t need any option for linking files unless you have a special purpose, but use “-s” options for linking directory.# Link "song.wav" file to "my_dir" directory.
ln song.wav ./my_dir
# Link "tmp_dir" to "my_dir" directory. Suppose "my_dir" is located in the "father" directory.
ln -s tmp_dir ./father/my_dir
find : It searches files and provides you the location of them.# Find "song.wav" files by searching the entire hard drive.
find / -name song.wav ("/" means the entire disk.)
# Find "song.wav" files by searching from current directories to its subdirectories (not outside of the current directory).
find . -name song.wav
# Find "song.wav" files by searching only the current directory.
find song.wav
grep : it catches specific characters or sentences from the text so the user is able to retrieve meaningful information.# find "information" word from the "test.txt" file.
grep 'information' test.txt
# find words that is NOT including "information" from the "test.txt" file. Use "-v" option.
grep -v 'information' test.txt
Hyungwon Yang
댓글
댓글 쓰기