DEV Community

yuki
yuki

Posted on

command line cheat sheet for newbies

A command-line interface (CLI) is a way to interact with a computer's operating system by typing commands into a terminal.

Structure

❯❯❯ $ command ${options} ${arguments}

# e.g.
❯❯❯ $ ls -la /document/photo 
Enter fullscreen mode Exit fullscreen mode
  • command
    • The action to be executed.
  • options
    • These are optional options (or flags) that modify the command's behaviour.
  • arguments
    • These are the required or optional arguments that use to specify the target. The format of arguments depend on the each command.

Shortcut

Shortcut Action
Ctrl + a Move the insertion point to the beginning of the line
Ctrl + e Move the insertion point to the end of the line
Ctrl + b Move the insertion point backwards
Ctrl + f Move the insertion point forward
Ctrl + h Delete one character behind the cursor
Ctrl + d Delete a character before the cursor
Ctrl + u Delete the entire line

Directory management

pwd

  • check the current directory
~/Desktop ❯❯❯ pwd
/Users/user/Desktop
Enter fullscreen mode Exit fullscreen mode

mkdir

  • make new directory
~/Desktop ❯❯❯ mkdir myDir
Enter fullscreen mode Exit fullscreen mode

cd

  • change directory
# change directory to myDir
~/Desktop ❯❯❯ cd myDir
# change directory one level up in the hierarchy
~/Desktop ❯❯❯ cd ..
# change directory to home
~/Desktop ❯❯❯ cd
Enter fullscreen mode Exit fullscreen mode

File operations

ls

  • list files and directories
  • -a: list all files including hidden file starting with '.'
  • -l: list with long format (permissions)
  • -t: sort by time and date
  • -d: show only the directories
~/Desktop ❯❯❯ ls -lat
total 2992
drwxr-xr-x+ 54 user  staff    1728  6 Sep 16:30 ..
drwx------@  9 user  staff     288  6 Sep 16:28 .
drwxr-xr-x@ 2 user  staff      64  6 Sep 16:28 newDir
-rw-r--r--@ 1 user  staff       0  6 Sep 16:25 file1.txt
-rwx------@ 1 user  staff  321110  6 Sep 14:04 file2.txt
-rwx------@ 1 user  staff  363448  5 Sep 08:46 file3.txt
-rwx------@ 1 user  staff  835488 26 Aug 11:21 file4.txt
Enter fullscreen mode Exit fullscreen mode

cp

  • copy files
  • -r: copy directories recursively
  • -p: preserve the attributes (mode,ownership and timestamps)
~/Desktop ❯❯❯ cp -r originalDir copyDir
Enter fullscreen mode Exit fullscreen mode

rm

  • remove files and directories
  • -r: delete directories recursively
  • -f: Ignore nonexistent files, and never prompt before removing
  • -i: Prompt before removal
  • -d: delete empty directories
~/Desktop ❯❯❯ rm -d originalDir
Enter fullscreen mode Exit fullscreen mode

mv

  • rename or move files and directories to another location
  • Adding a -v flag is to print source/destination directory
~/Desktop ❯❯❯ mv -v original.txt new.txt
original.txt -> new.txt
Enter fullscreen mode Exit fullscreen mode

other

man

  • view manual pages for Unix commands
  • press q to exit
~/Desktop ❯❯❯ man ls
LS(1)                       General Commands Manual                      LS(1)

NAME
     ls – list directory contents

SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]
        [-D format] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls
.
.
.
Enter fullscreen mode Exit fullscreen mode

clear

  • clear screen
  • shortcut is Ctrl + k

history

  • show history of previous commands
  300  ls -lat
  301  pwd
  302  cd
  303  man ls
Enter fullscreen mode Exit fullscreen mode

★ extra tip! ★

If you want to make sure a prompt always appears before deleting files you can create alias by changing configuration file.

  • .bashrc: Bash shell
  • .zshrc: Zsh shell
~ ❯❯❯ echo 'alias "rm=rm -i"' >> ~/.zshrc  
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
mohiyaddeen7 profile image
mohiyaddeen7

Nice. Please post a blog on vs code shortcuts too.✌️