0225 Shell Commands

Youngin Lee·2021년 2월 25일
0

Windows vs Linux

Windows: one root for every partition (C:\, D:\ etc.)

Linux & MacOS: mounted under one namespace

basic commands

  • echo display line of text/string that are passed as an argument

  • pwd print working directory

  • ls list the files in the current directory

  • cd ~ (tilde) change directory to home directory

	user_id@your_system:~$ pwd
	/home/user_id
  • cd - change directory to previous directory

  • man takes another program as an argument and print its manual page

  • cat prints the content of a file

flags and options

  • -l listing format

  • -al do not ignore entries starting with .

	user_id@your_system:~$ cd /
	user_id@your_system:/$ ls
	bin   dev   home   lib   lib64   media   opt   root   sbin   srv   tmp   var...
	user_id@yoursystem:/$ ls -al
	total 620
	drwxr-xr-x  1 root root    512 Feb 25 20:52 .
	drwxr-xr-x  1 root root    512 Feb 25 20:52 ..
	lrwxrwxrwx  1 root root      7 Aug  5  2020 bin -> usr/bin
	drwxr-xr-x  1 root root    512 Aug  5  2020 boot
	drwxr-xr-x  1 root root    512 Feb 25 20:52 dev
	drwxr-xr-x  1 root root    512 Feb 25 20:52 etc
	drwxr-xr-x  1 root root    512 Feb 15 19:44 home
	-rwxr-xr-x  1 root root 631968 Dec  8  2019 init
	lrwxrwxrwx  1 root root      7 Aug  5  2020 lib -> usr/lib
	lrwxrwxrwx  1 root root      9 Aug  5  2020 lib32 -> usr/lib32
	lrwxrwxrwx  1 root root      9 Aug  5  2020 lib64 -> usr/lib64
	lrwxrwxrwx  1 root root     10 Aug  5  2020 libx32 -> usr/libx32
	drwxr-xr-x  1 root root    512 Aug  5  2020 media
	drwxr-xr-x  1 root root    512 Feb 15 19:43 mnt
	drwxr-xr-x  1 root root    512 Aug  5  2020 opt
	.
	.
	.
  • /usr/bin/. all of the files in this directory have the execute bit set. even for people who are not the owner of the file. ex: /usr/bin/echo
  • Linux permissions - File mode
    Command File Type
    - file
    d directory
    l symbolic link
  • File mode
    Command Meaning
    r read
    w write
    x execute
    - not allowed
  • The first 3 letters show the permissions for the file owner, the second 3 letters show the permissions for the group owner and the last 3 letters show the permissions for other users.
	rwx/ r-x/ r-x/

	Owner: rwx
	Group: r-x
	Users: r-x
  • file r : read
    file w : save, edit
    file x : excute

  • directory r : see files inside the directory
    directory w : rename, create, list
    directory x : search, are you allowed to enter this directory

  • to cd into a directory, you must have the execute permission on all parent directories of that directory and the directory itself.

	/usr/bin/echo
       ↑  ↑  ↑
      --x--x--x

input content into a file

  • > < pass the content to the shell input / shell output
    user_id@your_system:~$ echo "hello world!">hello.txt
    user_id@your_system:~$ ls
    hello.txt
    user_id@your_system:~$ cat hello.txt
    hello world!
    user_id@your_system:~$ cat hello.txt > hello2.txt
    user_id@your_system:~$ cat hello2.txt
    hello world!
    
  • >> append the content instead of just overwrite
    user_id@your_system:~$ cat < hello.txt > hello2.txt
    user_id@your_system:~$ cat hello2.txt
    hello world!
    user_id@your_system:~$ cat < hello.txt >> hello2.txt
    user_id@your_system:~$ cat hello2.txt
    hello world!
    hello world!

cat vs cat <

Difference between "cat" and "cat<" - Stack Exchange 🔗

In the first case, cat opens the file, and in the second case, the shell opens the file, passing it as cat's standard input.
Technically, they could have different effects. For instance, it would be possible to have a shell implementation that was more (or less) privileged than the cat program. For that scenario, one might fail to open the file, while the other could.

the pipe character |

  • | takes the output of the program and to the left, and make it the input of the program to the right.

  • tail prints the last n lines of its input

    tail -n1

if you want to print only the last line of your output, you can wire these together using pipe line.

    user_id@your_system:~$ ls -l /
    total 620
    lrwxrwxrwx  1 root root      7 Aug  5  2020 bin -> usr/bin
    drwxr-xr-x  1 root root    512 Aug  5  2020 boot
    .
    .
    .
    drwxr-xr-x  1 root root    512 Aug  5  2020 usr
    drwxr-xr-x  1 root root    512 Aug  5  2020 var
    user_id@your_system:~$ ls -l / | tail -n1
    drwxr-xr-x  1 root root    512 Aug  5  2020 var

| would wire two different programs that have never been programmed to be compatible with one another.
the command is basically saying "I want the output of ls to be the input to tail and I want the output of tail to just go to my terminal."

you can also wire the output of the tail into the txt file.

    user_id@your_system:~$ ls -l / | tail -n1 >> hello2.txt
    user_id@your_system:~$ cat hello2.txt
    hello world!
    hello world!
    drwxr-xr-x  1 root root    512 Aug  5  2020 var

some more wiring

  • curl gives the all the HTTP headers for accessing the website
    user_id@your_system:~$ curl --head --silent google.com
    HTTP/1.1 301 Moved Permanently
    Location: http://www.google.com/
    Content-Type: text/html; charset=UTF-8
    Date: Sat, 27 Feb 2021 08:27:20 GMT
    Expires: Mon, 29 Mar 2021 08:27:20 GMT
    Cache-Control: public, max-age=2592000
    Server: gws
    Content-Length: 219
    X-XSS-Protection: 0
    X-Frame-Options: SAMEORIGIN
  • grep lets you search in an input stream for a given keyword

  • wire these two program

    user_id@your_system:~$ curl --head --silent google.com | grep -i content-length
    Content-length: 219
  • wire cut command as well
    user_id@your_system:~$ curl --head --silent google.com | grep -i content-length | cut --delimiter=' ' -f2
    219

chaining these programs together, you can achieve a bunch of really interesting text manipulation effect.

in fact, pipes are not just for textual datas. you can do these things with binary images, or streaming a video file.

how to use terminal in a more powerful, useful way

  • root user
    is sort of like administrater user on Windows.
    root user is allowed to do whatever it wants on your system.

  • sudo do the following things as a superuser

  • /sys file system

   user_id@your_system:~$ cd /sys
   user_id@your_system:/sys$ ls
   block  bus  class  dev  devices  firmware  fs  kernel  module  power

this file system is not actually files on your computer, instead these are various kernel parameters. (the kernel is the core of your computer.)

  • # pound symbol
    indicates you are running the command as root
  • $ dollar symbol
    indicates you are not running the command as root
    user_id@your_system:~$ sudo su
    [sudo] password for user_id:
    root@your_system:/home/user_id#

some useful commands

  • ctrl+c interrupt the line
  • ctrl+l clear the kernel line
  • ctrl+a go to the beginning of the line you're currently typing on
profile
🤔🤔

0개의 댓글