Aller au contenu

Linux cheatsheet

Overview

The basic concepts of working in a Linux operating system are files and directories (folders) organized in a tree structure within an environment.

Once you login to a Linux system, you are working in a shell in which you can work on files and directories, by executing commands which are installed on the system. The Bash shell is a common and popular shell which is typically found on Linux systems.

Bash

Directory Navigation

  • Entering an absolute directory:
cd /dir1/dir2
  • Entering a relative directory:
cd ./somedir
  • Move one directory up:
cd ..
  • Move two directories up:
cd ../..
  • Move to your "home" directory:
cd -

File Management

  • Listing files in the current directory:
ls
  • Listing files in the current directory with more detail:
ls -l
  • List the root of the filessystem:
ls -l /
  • Create an empty file:
touch foo.txt
  • Create a file from an echo command:
echo "hi there" > test-file.txt
  • View the contents of a file:
cat test-file.txt
  • Copy a file:
cp file1 file2
  • Wildcards: operate on file patterns:
ls -l fil*  # matches file1 and file2
  • Concatenate two files into a new file called newfile:
cat file1 file2 > newfile
  • Append another file into newfile
cat file3 >> newfile
  • Delete a file:
rm newfile
  • Delete all files with the same file extension:
rm *.dat
  • Create a directory
mkdir dir1

Chaining commands together with pipes

Pipes allow a user to send the output of one command to another using the pipe | symbol:

echo "hi" | sed 's/hi/bye/'
  • Filtering command outputs using grep:
echo "id,title" > test-file.txt
echo "1,birds" >> test-file.txt
echo "2,fish" >> test-file.txt
echo "3,cats" >> test-file.txt

cat test-file.txt | grep fish
  • Ignoring case:
grep -i FISH test-file.txt
  • Count matching lines:
grep -c fish test-file.txt
  • Return outputs not containing keyword:
grep -v birds test-file.txt
  • Count the number of lines in test-file.txt:
wc -l test-file.txt
  • Display output one screen at a time:
more test-file.txt

...with controls:

  • Scroll down line by line: enter
  • Go to next page: space bar
  • Go back one page: b

  • Display the first 3 lines of the file:

head -3 test-file.txt
  • Display the last 2 lines of the file:
tail -2 test-file.txt