Pages

Windows Guy Learning Linux - Basic Command for Linux

If you have been working mostly with Microsoft technology stacks, switching from Windows and learning Linux could be an exciting and daunting journey. So far my journey to Linux has been fun and I love it!

Linux has GUI but mostly (almost entirely) for server and application administration are performed using command line. If you are on Windows workstation, PuTTY software could be used to ssh into Linux server.

Here are some basic and commonly used commands. Hopefully the list is useful for you. Have fun learning!

Terminal
ctrl+c  stop the command.
ctrl+d  log out from current terminal (just like the command exit)
ctrl+u  erase the current line. It clear from the cursor to the beginning of line
ctrl+y  redo current line text from ctrl+u
ctrl+l  clear screen (or use clear command). Just like cls in windows
ctrl+z  send the process to background. Useful when you have a long running process and need to work on terminal. type fg to bring it back

Not that useful on windows keyboard
ctrl+a  move the cursor to beginning of line. Or just use keyboard home button
ctrl+e  move the cursor to end of line. Or just use keyboard end button

Tips: tab button helps automatically complete the command or file. Just like in cmd or powershell.


Help / Information
man  user manual or help for command. eg. man ls show details and usage of ls command. To get out of man page, type q


Commonly use command
pwd  current folder path
ls  list directory content. eg.
ll  is an alias of ls -l. It shows long listing format
ls -al  includes showing long listing format including file starting with . (dot). This file is treated as hidden file.

Use ll command to list file / directory permission. More permission info here. For example,

drwxr-xr-x  (a directory)
-rw-r--r--  (a file)

r as read permission
w as modify permission
x as execute file or view content of directory permission

1st character indicates a file or directory. d as directory, - as file
2-4 characters indicate the permission of owner of the file/directory
5-7 characters indicate the permission of the group of the file/directory
8-10 characters indicate the permission of all other users

chown user1.group1 <file/folder>  change ownership of files/directories. User, group, other (every users). Use -R as recursive for folder permission change
chmod 644 <file/folder>  change file/folder permission. eg. If this is a file, 6 as binary is 110, 4 as binary as 100. The file permission would be -rw-r--r--
sudo  execute a command as superuser or another user
su  change user

cd change current directory. Just like windows
cd -  (dash), cd into previous folder (folder before the previous cd command)
cd ~  (tilde), ~ refer to home directory. cd into home directory
cd /  (slash), / is the root directory
md  make (create) new directory
rm  remove folder or file. eg. rm <file>.. rm -r <folder> (r is to remove directory and its contents recursively)
cp  copy file/folder
scp  secure copy file/folder. eg copying files between hosts using ssh. eg. scp file.txt username@destination_host:/folder/subfolder
touch <file>   create empty file. Or use text editor to create new file with content
output to file. eg. ls > abc.txt
>>  append to file. eg. ls >> abc.txt
find <directory> -name <file/folder>  search file/folder in the directory. use -iname for insensitive search
locate <file>  quickly search through files index db on the system (maybe not up to date)
file <file>  show file type (eg. examine if a file is text or archive file)
grep  search matching pattern or regular expression. Very useful for matching pattern. Eg. using in conjunction with ps -ef | grep java, to find java process

Archive/Compress tar  archive utility (to zip or/and compress). Eg.
tar -cvf  test.tar test1  (to archive test1 folder into a tar file. -c as archive, -v for verbose and -f for file
tar -xvf  test.tar (to extract all files from archive.tar)
tar -cvzf  test.tar.gz test1 (to archive test1 folder and compress into tar.gz file) -z for compress
tar -xvzf test.tar.gz (to extract and uncompress archive.tar.gz)

View / modify file content
cat <file>  concatenate and show all contents
less <file>  show content one page at a time. Scroll forward with f (or page down button) and scroll backward with b (or page up button).
less -p <pattern>  <file>  find pattern in the file
nano  simple text editor. kind of like notepad. nano is pretty straight forward. For help, press ctrl+G. The ^ (caret) refers to ctrl key. M refer to alt key
vi  more advanced (could be unintuitive for beginner) text editor. vi has two modes of operation: command mode and insert mode

press Esc key to put vi in command mode
press i key to put vi in insert mode

vi command mode,
dd  delete line
yy  copy line. Use mouse to select the word for
p  paste
u  undo
ctrl+r  redo
1G  beginning of first line (or :0)
G$  end of last line
:/searchword  find word (n for next found word, shift+n for previous)
:%s/<searchword>/<replaceword>/g  find searchword and replace with replaceword
:q  exit
:q!  exit and cancel any change
:wq  save and exit

System (storage, process, services, etc)
uname -a  show all system information eg linux version
df -h  display file system disk space usage. -h for human readable size (eg. MB, GB instead of bytes)
du -sh <folder>  display file usage of a folder. -s for summary and -h for human readable size
ps -ef  display a snapshot of current process. -ef to show all process using standard syntax
top  display linux processes. Like task manager in windows. Example,
h for help
z highlight
c show absolute path
k to kill process pid
u filter user

systemctl -at service systemctl is used to manage services. Show all services
systemctl -t service --state=active Show only active services
systemctl status <service>  show service status (eg. enabled, active)
sudo systemctl start <service>  start specific service
sudo systemctl stop <service>  stop specific service

More administrative commands could be found on this page.

No comments:

Post a Comment