CodeNewbie Community

Cover image for Linux Terminal - The Ultimate Cheat Sheet - Part 2
Mauro Garcia
Mauro Garcia

Posted on • Originally published at maurogarcia.dev

Linux Terminal - The Ultimate Cheat Sheet - Part 2

This post is the second part of a series that will help you learn how to benefit from the Linux Terminal.
If you didn't see my first post, I highly recommend taking a look at it before continuing with this one:

💡 If you found this content valuable, you can follow me on Twitter to get updates about upcoming posts

TL;DR

  • Search with Grep
    • Search for a string within a file ➜ grep [term-to-search] [source-file-to-search]
    • Case Insensitive Search within a file ➜ grep -i [term-to-search] [source-file-to-search]
    • Search for non-matching lines within a file ➜ grep -v [term-to-search] [source-file-to-search]
    • Recursive search within a directory ➜ grep -r [term-to-search] [path-to-directory-to-search]
    • Multiple searches within a file ➜ grep -E "[first-term-to-search|second-term-to-search]" [source-file-to-search]
    • Count search results ➜ grep -c [term-to-search] [source-file-to-search]
    • Showing the name of the matching files ➜ grep -l [term-to-search] [matching-files-to-search]
    • Learn more about grep ➜ man grep

  • Pipes
    • Piping commands ➜ [command 1] | [command 2] | [command n]
    • Piping filtered search results into a new file ➜ ls | grep [term-to-filter] | cat > [path-to-new-file]/[name-for-new-file]
    • Searching through your command history ➜ history | grep "[term-to-search]"

  • Permissions: Change file mode bits command (chmod)
    • Add execute permission to everyone ➜ chmod a+x [name-of-the-file] or chmod +x [name-of-the-file]
    • Remove execute permission to everyone ➜ chmod a-x [name-of-the-file] or chmod -x [name-of-the-file]
    • Add execute permission to the owner ➜ chmod u+x [name-of-the-file]
    • Remove write permission to others users ➜ chmod o-w [name-of-the-file]
    • Add read permission to the group ➜ chmod g+r [name-of-the-file]
    • Remove write and read permission to everyone ➜ chmod a-wr [name-of-the-file]
    • Remove write and read permission to everyone for all the files in the current directory ➜ chmod a-wr *.*

  • Groups
    • List all the available groups ➜ getent group
    • List all the groups my account is assigned to ➜ groups
    • Search for a specific group (using pipes) ➜ getent group | grep [group-name-to-search]
    • Create a new group ➜ sudo groupadd [name-for-the-new-group]
    • Add an existing user to a secondary group ➜ usermod -a -G [group-you-want-to-add-the-user-to] [user-name-to-add]

  • Ownership: Change file owner and group (chown)
    • Change user ownership for a file ➜ sudo chown [new-owner-name] [file-to-change-ownership]
    • Change user ownership for several files ➜ sudo chown [new-owner-name] [file-1-to-change-ownership] [file-n-to-change-ownership]
    • Change user ownership for a directory ➜ sudo chown [new-owner-name] [directory-to-change-ownership]
    • Recursively change user ownership for a directory and all it's files ➜ sudo chown -R [new-owner-name] [directory-to-change-ownership]
    • Change group ownership for a file ➜ sudo chown :[new-group-name] [file-to-change-ownership]
    • Change user and group ownership for a file ➜ sudo chown [new-owner-name]:[new-group-name] [file-to-change-ownership]

  • Shortcuts
    • Search through your search history ➜ [CTRL] + r. Then type a few characters to find you command
    • Paste previous lines ➜ [CTRL] + p
    • Moves the cursor to the beginning of the line. ➜ [CTRL] + a
    • Moves the cursor to the end of the line. ➜ [CTRL] + e
    • Moves the cursor forward one character. ➜ [CTRL] + f
    • Moves the cursor backward one character. ➜ [CTRL] + b
    • Erases the complete line. ➜ [CTRL] + u
    • Erases the last word typed. ➜ [CTRL] + w

  • Working with long files
    • Print the last lines for a file ➜ tail [name-of-the-file]
    • Print the last n lines for a file ➜ tail -n [number-of-lines] [name-of-the-file]
    • Print the first lines for a file ➜ head [name-of-the-file]
    • Print the first n lines for a file ➜ head -n [number-of-lines] [name-of-the-file]
    • Page through a file ➜ less [name-of-the-file]

Global regular expression search (grep command)

The Linux grep command is a string and pattern matching utility that displays matching lines from multiple files.

Search for a string within a file

Type grep [term-to-search] [source-file-to-search] to search for a term within a file. You can optionally add the -n argument if you want to print the line number for each match

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt

## Print the content of the index.html file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>My Website</title>
  </head>

  <body>
    <script src="js/main.js"></script>
  </body>
</html>

## Search for "meta" within the index.html file using grep
mauro_codes@mauro-desktop:~/projects/landing-page$ grep meta index.html -n
4:    <meta charset="utf-8" />
5:    <meta http-equiv="x-ua-compatible" content="ie=edge" />
6:    <meta name="viewport" content="width=device-width, initial-scale=1" />
Enter fullscreen mode Exit fullscreen mode

Case Insensitive Search within a file

Type grep -i [term-to-search] [source-file-to-search] to perform a case insensitive search:

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

## Search for "sum" without ignoring case (we get 2 results)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -n
1:const sum = (num1, num2) => {
6:sum(10,4)

## Search for "sum" using the ignore case argument (we get 3 results)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -in
1:const sum = (num1, num2) => {
5:// Call the Sum function
6:sum(10,4)
Enter fullscreen mode Exit fullscreen mode

Search for non-matching lines within a file

Type grep -v [term-to-search] [source-file-to-search] to get all the lines within a file that don't match with the term

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

## Search for each line that doesn't include "sum" (case sensitive)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -v
        return num1 + num2
}

// Call the Sum function
Enter fullscreen mode Exit fullscreen mode

Recursive search within a directory

Type grep -r [term-to-search] [path-to-directory-to-search] to search through nested directories and subdirectories.

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt  temp

## Search recursively for "sum" within the current directory (including sub-directories)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -r sum .
./index.html:    <p>Calling the sum function:<p>
./index.html:    <!-- TODO: Call the sum function and display the value -->
./main.js:const sum = (num1, num2) => {
./main.js:sum(10,4)
./temp/index.html:    <p>Calling the sum function:<p>
./temp/index.html:    <!-- TODO: Call the sum function and display the value -->
Enter fullscreen mode Exit fullscreen mode

Multiple searches within a file

Type grep -E "[first-term-to-search|second-term-to-search]" [source-file-to-search] to search for multiple terms within a file

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

const printMessage(message) {
        console.log(message)
}

// Call the printMessage function
printMessage("Hello world")

## Search for "sum" or "printMessage" within the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -E "sum|printMessage" main.js
const sum = (num1, num2) => {
sum(10,4)
const printMessage(message) {
// Call the printMessage function
printMessage("Hello world")
Enter fullscreen mode Exit fullscreen mode

Count search results

Type grep -c [term-to-search] [source-file-to-search] to count how many times a search term appears in the file

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt  temp

## Count how many times "sum" appears on the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c sum main.js
2

## Count how many times "sum" or "printMessage" appears on the main.js file 
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c -E "sum|printMessage" main.js
5

## Recursive count to get how many times "sum" or "printMessage" appears in the current working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c -E -r "sum|printMessage" .
./index.html:2
./main.js:5
./README.md:0
./script.txt:0
./temp/index.html:2
Enter fullscreen mode Exit fullscreen mode

Showing the name of the matching files

Type grep -l [term-to-search] [matching-files-to-search] to get a list of files that includes the search term

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  about.html  index.html  main.js  script.txt  temp

## Get a list of html files that contains "<h1>"
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l "<h1>" *.html
index.html

## Get a list of html files that contains "<p>"
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l "<p>" *.html
about.html
index.html

## Recursively get a list of files that contains "<p>" within the current directory
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l -r "<p>" .
./about.html
./index.html
./temp/index.html
Enter fullscreen mode Exit fullscreen mode

Pipes

Pipes are among the most useful command-line features on Linux and allow you to perform complex operations by "piping" multiple operations together, which means that the output of each command within the pipe will serve as an input for the next command.

Piping filtered search results into a new file

Let's say I want to get a list of all my blog posts that have the word "svelte" within the title. And I want to write the full list on a new file called svelte-articles.txt.

We can use pipes to list all my articles, filter those that include "svelte" using grep, and save that filtered list on a new file.

## Check that my current working directory is the "posts" folder that includes all my posts
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ pwd
/home/mauro_codes/projects/maurogarcia.dev/posts

## Piping the list of posts with grep to check how many posts include the term "svelte" (I get five items)
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ ls | grep svelte
5

## Piping the list of post --> Filter svelte posts --> move the content to a new file using cat
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ ls | grep svelte | cat > /home/mauro_codes/projects/svelte-articles.txt

## Print the content of our new file
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ cat /home/mauro_codes/projects/svelte-articles.txt
-rw-r--r-- 1 mauro_codes mauro_codes 4.2K Jan 27 18:29 -5-things-i-love-about-svelte-.md
-rw-r--r-- 1 mauro_codes mauro_codes  11K Jan 27 18:29 angular-vs-svelte-card-component.md
-rw-r--r-- 1 mauro_codes mauro_codes 5.3K Jan 27 18:29 component-driven-development-with-svelte.md
-rw-r--r-- 1 mauro_codes mauro_codes 5.0K Jan 27 18:29 how-to-build-your-next-chrome-extension-with-svelte.md
-rw-r--r-- 1 mauro_codes mauro_codes 4.7K Jan 27 18:29 sapper-svelte-tailwindcss-boilerplate.md
Enter fullscreen mode Exit fullscreen mode

Searching through your command history

You can use pipes to search through your command history using grep by typing history | grep "[term-to-search]"

Thanks to @bradnichol for suggesting this approach in my previous post!

## Searching for the term svelte on your command history
mauro_codes@mauro-desktop:~$ history | grep "svelte"
  415  ls | grep svelte
  416  ls | grep -C svelte
  417  ls | grep -c svelte
  418  ls | grep -c svelte |
  419  ls | grep -c svelte | cat
  420  ls | grep svelte | cat
  421  ls | grep svelte | cat > /home/mauro_codes/projects/svelte-articles.txt
  422  cat /home/mauro_codes/projects/svelte-articles.txt
  444  ls grep -c svelte
  445  ls | grep -c svelte
  446  cat /home/mauro_codes/projects/svelte-articles.txt
  448  history | grep "svelte"
Enter fullscreen mode Exit fullscreen mode

Permissions: Change file mode bits command (chmod)

Changing permissions in Linux is an extensive topic that deserves its own post. But I decided to include a few examples that can come in handy.

You can use the chmod command to add or remove the read, write, and execute permissions to different users for one or more files.

As a reference, here is an example of how the permissions are displayed for each file or directory when we use ls lah

Linux permissions

Add the execute permission to everyone

Type chmod a+x [name-of-the-file] or chmod +x [name-of-the-file] to add execute permission to everyone for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add execute permission to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod +x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Remove the execute permission to everyone

Type chmod a-x [name-of-the-file] or chmod -x [name-of-the-file] to remove execute permission to everyone for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the execute permissions to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rw-rw-rw- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Add the execute permission to the owner

Type chmod u+x [name-of-the-file] to add the execute permission only to the owner for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add the execute permissions only to the owner in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod u+x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x------ 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Remove write permission to other users

Type chmod o-w [name-of-the-file] to remove the write permission only to other users (not owner or group) for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write permission only to other users (not owner or group) in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod o-w main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxr-x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Add read permission to the group

Type chmod g+r [name-of-the-file] to add read permission only to the group for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add the read permission only to the group in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod g+r main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---------r----- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Remove write and read permission to everyone

Type chmod a-wr [name-of-the-file] to remove write and read permissions to everyone for a specific file

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write and read permission to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-wr main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Remove write and read permission to everyone for multiple files

Type chmod a-wr *.* to remove the write and read permissions to everyone for all the files in the current working directory.

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write and read permissions to everyone for all the files in the current working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-wr *.*

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Groups

List all the available groups

Type getent group to get a list including all the available groups in your system.

mauro_codes@mauro-desktop:~/projects/landing-page$ getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
mauro_codes:x:1000:
Enter fullscreen mode Exit fullscreen mode

List all the groups my account is assigned to

Type groups to get a list of all the groups you are part of.

## List all my groups
mauro_codes@mauro-desktop:~/projects/landing-page$ groups
mauro_codes adm dialout cdrom floppy sudo audio dip video plugdev netdev
Enter fullscreen mode Exit fullscreen mode

Search for a specific group (using pipes)

Type getent group | grep [group-name-to-search] to search for a specific group using pipes.

## List all the groups that contains the term "mauro_codes"
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep "mauro_codes"
adm:x:4:syslog,mauro_codes
mauro_codes:x:1000:

## List all the groups that contains the term "root"
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep "root"
root:x:0:
Enter fullscreen mode Exit fullscreen mode

Create a new group

Type sudo groupadd [name-for-the-new-group] to create a new group.

## Create a new group called "development"
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo groupadd development

## Check if the new group was created
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep development
development:x:1001:
Enter fullscreen mode Exit fullscreen mode

Add an existing user to a secondary group

Type usermod -a -G [group-you-want-to-add-the-user-to] [user-name-to-add] to add an existing user to a secondary group.

## Add mauro_codes to the secondary group "development"
mauro_codes@mauro-desktop:~/projects$ sudo usermod -a -G development mauro_codes

## Check changes
mauro_codes@mauro-desktop:~/projects$ getent group | grep "mauro"
adm:x:4:syslog,mauro_codes
dialout:x:20:mauro_codes
mauro_codes:x:1000:
development:x:1001:mauro_codes
Enter fullscreen mode Exit fullscreen mode

Ownership: Change file owner and group (chown)

Change user ownership for a file

Type sudo chown [new-owner-name] [file-to-change-ownership] to change user ownership for a file.

## Check user ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user ownership (the root user will be the owner of the main.js file)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root main.js

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Change user ownership for several files

Type sudo chown [new-owner-name] [file-1-to-change-ownership] [file-n-to-change-ownership] to change user ownership for several files at the same time.

## Check user ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user ownership (the root user will be the owner of the main.js and script.txt files)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root main.js script.txt

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 root        mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Change user ownership for a directory

Type sudo chown [new-owner-name] [directory-to-change-ownership] to change the user ownership for a directory.

## Check directories user ownership
mauro_codes@mauro-desktop:~/projects$ ls -lah
total 4.0K
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 28 10:48 ..
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 landing-page
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 22 12:15 personal-blog

## Change user ownership (the root user will be the owner of the "landing-page" directory)
mauro_codes@mauro-desktop:~/projects$ sudo chown root ./landing-page/

## Check directories user ownership again
mauro_codes@mauro-desktop:~/projects$ ls -lah
total 4.0K
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 28 10:48 ..
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 landing-page
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 22 12:15 personal-blog
Enter fullscreen mode Exit fullscreen mode

Recursively change user ownership for a directory and all its files

Type sudo chown -R [new-owner-name] [directory-to-change-ownership] to recursively change the user ownership for a directory and all its files.

## Check files ownershop within the landing-page directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 16:27 temp_dir

## Recursively change the user ownership (the root user will be the owner 
## of all the files and directories within the current directory)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown -R root .

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 root        mauro_codes  23 Jan 24 17:31 script.txt
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 16:27 temp_dir
Enter fullscreen mode Exit fullscreen mode

Change group ownership for a file

Type sudo chown :[new-group-name] [file-to-change-ownership] to change the group ownership for a file.

## Check group ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change group ownership (the main.js file will now have "development" as a group)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown :development main.js

## Check group ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes development 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Change user and group ownership for a file

Type sudo chown [new-owner-name]:[new-group-name] [file-to-change-ownership] to change the user and the group ownership for a file at the same time.

## Check ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user and group ownership (the main.js file will now have "development" as a group and root as owner)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root:development main.js

## Check ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
--------x--x--x 1 root        development 206 Jan 27 16:36 main.js
--------x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

Shortcuts

Big shout-out to @bogkonstantin and @samuelabreu for letting me know about these commands I didn't know.

Search through your command history

In the previous post, we saw that we could get a full list of the latest commands we run by typing history. If you don't want to see the full list but search for a specific command quickly, type [CTRL] + r. You'll be asked to enter a few characters to get matching commands. You can iterate between those results by typing [CTRL] + r again.

Moving through your commands

There are a few shortcuts you can use to move through your commands:

  • Paste previous lines ➜ [CTRL] + p
  • Moves the cursor to the beginning of the line. ➜ [CTRL] + a
  • Moves the cursor to the end of the line. ➜ [CTRL] + e
  • Moves the cursor forward one character. ➜ [CTRL] + f
  • Moves the cursor backward one character. ➜ [CTRL] + b
  • Erases the complete line. ➜ [CTRL] + u
  • Erases the last word typed. ➜ [CTRL] + w

Working with long files

  • Print the last lines for a file ➜ tail [name-of-the-file]
  • Print the last n lines for a file ➜ tail -n [number-of-lines] [name-of-the-file]
  • Print the first lines for a file ➜ head [name-of-the-file]
  • Print the first n lines for a file ➜ head -n [number-of-lines] [name-of-the-file]
  • Page through a file ➜ less [name-of-the-file]

Let me know if you want me to cover any particular command on a final post!

Discussion (0)