Linux Bash Commands

¡Supera tus tareas y exámenes ahora con Quizwiz!

type <command>

A command can be one of those 4 types: an executable a shell built-in program a shell function an alias The type command can help figure this out, in case we want to know or we're just curious. It will tell you how the command will be interpreted.

What is an environment variable

An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

uname

Calling uname without any options will return the Operating System codename

crontab -l

Cron jobs are jobs that are scheduled to run at specific intervals. You might have a command perform something every hour, or every day, or every 2 weeks. Or on weekends. They are very powerful, especially when used on servers to perform maintenance and automations. The crontab command is the entry point to work with cron jobs

chmod

Every file in the Linux / macOS Operating Systems (and UNIX systems in general) has 3 permissions: read, write, and execute. You can change the permissions given to a file using the chmod command.

chown <owner> <file> chown flavio test.txt

Every file/directory in an Operating System like Linux or macOS (and every UNIX system in general) has an owner. The owner of a file can do everything with it. It can decide the fate of that file. The owner (and the root user) can change the owner to another user, too, using the chown command

history history | grep docker # displays commands with docker history -c

Every time you run a command, it's memorized in the history. You can display all the history. Typically the last 500 commands are stored in the history. -c clears history.

find . -type d -name src find . -type f find . -type l

Find directories under the current tree matching the name "src". Use -type f to search only files, or -type l to only search symbolic links. -name is case sensitive. use -iname to perform a case-insensitive search.

grep -n document.getElementById index.md

For example here's how we can find the occurences of the document.getElementById line in the index.md file.Using the -n option it will show the line numbers.

kill <PID> kill -HUP <PID> kill -INT <PID> kill -KILL <PID> kill -TERM <PID> kill -CONT <PID> kill -STOP <PID>

HUP means hang up. It's sent automatically when a terminal window that started a process is closed before terminating the process. INT means interrupt, and it sends the same signal used when we press ctrl-C in the terminal, which usually terminates the process. KILL is not sent to the process, but to the operating system kernel, which immediately stops and terminates the process. TERM means terminate. The process will receive it and terminate itself. It's the default signal sent by kill. CONT means continue. It can be used to resume a stopped process. STOP is not sent to the process, but to the operating system kernel, which immediately stops (but does not terminate) the process.

ln recipes.txt newrecipes.txt ln <original> <link>

Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't link to external filesystems (disks). A hard link is created using the following syntax. For example, say you have a file called recipes.txt. You can create a hard link to it. The new hard link you created is indistinguishable from a regular file. Now any time you edit any of those files, the content will be updated for both. If you delete the original file, the link will still contain the original file content, as that's not removed until there is one hard link pointing to it.

touch pear touch apple mkdir fruits mv pear apple fruits #pear and apple moved to the fruits folder

If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved in the folder path identified by the last parameter.

grep

If you're wondering, grep stands for global regular expression print. You can use grep to search in files, or combine it with pipes to filter the output of another command.

printenv

In any shell there are a good number of environment variables, set either by the system, or by your own shell scripts and configuration. You can print them all to the terminal using the printenv command

cat file cat file1 file2 cat file1 file2 > file3 cat file1 file2 >> file3 cat -n file1

In its simplest usage, cat prints a file's content to the standard output. You can print the content of multiple files. and using the output redirection operator > you can concatenate the content of multiple files into a new file. Using >> you can append the content of multiple files into a new file, creating it if it does not exist. When you're looking at source code files it's helpful to see the line numbers. You can have cat print them using the -n option. You can only add a number to non-blank lines using -b, or you can also remove all the multiple empty lines using -s. cat is often used in combination with the pipe operator | to feed a file's content as input to another command: cat file1 | anothercommand.

chown -R <owner>:<group> <file> chgrp <group> <filename>

It's rather common to need to change the ownership of a directory, and recursively all the files contained, plus all the subdirectories and the files contained in them, too. Files/directories don't just have an owner, they also have a group. Through this command you can change that simultaneously while you change the owner. You can also just change the group of a file using the chgrp command.

ln

It's used to create links. What is a link? It's like a pointer to another file, or a file that points to another file. You might be familiar with Windows shortcuts. They're similar. We have 2 types of links: hard links and soft links.

mv pear new_pear

Once you have a file, you can move it around using the mv command. You specify the file current path, and its new path. The pear file is now moved to new_pear. This is how you rename files and folders.

grep -nC 2 document.getElementById index.md

One very useful thing is to tell grep to print 2 lines before and 2 lines after the matched line to give you more context. That's done using the -C option, which accepts a number of lines. Search is case sensitive by default. Use the -i flag to make it insensitive. Use -v to invert the result.

ln -s recipes.txt newrecipes.txt ln -s <original> <link>

Soft links are different. They are more powerful as you can link to other filesystems and to directories. But keep in mind that when the original is removed, the link will be broken. You create soft links using the -s option of ln. In this case you can see there's a special l flag when you list the file using ls -al. The file name has a @ at the end, and it's also colored differently if you have colors enabled. Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or directory" if you try to access it.

which <command>

Suppose you have a command you can execute, because it's in the shell path, but you want to know where it is located. You can do so using which. The command will return the path to the command specified. Which will only work for executables stored on disk, not aliases or built-in shell functions.

sort dogs.txt sort -r dogs.txt

Suppose you have a text file which contains the names of dogs, this list is unordered. The sort command helps you sort them by name. Use the r option to reverse the order. Sorting by default is case sensitive, and alphabetic. Use the --ignore-case option to sort case insensitive, and the -n option to sort using a numeric order.You can use the -u option to remove duplicates.

env USER=flavio node app.js

Suppose you want to run a Node.js app and set the USER variable to it. You can run env USER=flavio node app.js and the USER environment variable will be accessible from the Node.js app via the Node process.env interface

gzip -kv filename

The -v option prints the compression percentage information. Here's an example of it being used along with the -k (keep) option

tail -f /var/log/system.log tail -n 10 <filename> tail -n +10 <filename>

The best use case of tail in my opinion is when called with the -f option. It opens the file at the end, and watches for file changes. Any time there is new content in the file, it is printed in the window. This is great for watching log files. You can print the last 10 lines in a file. You can print the whole file content starting from a specific line using + before the line number.

df df -h df <filename or directory>

The df command is used to get disk usage information. Using the -h option (df -h) will show those values in a human-readable format. You can also specify a file or directory name to get information about the specific volume it lives on:

du du -m du -g du -h du *

The du command will calculate the size of a directory as a whole (in bytes). The -h option will show a human-readable notation for sizes, adapting to the size. You can set du to display values in MegaBytes using du -m, and GigaBytes using du -g. Running du * will calculate the size of each file individually. Adding the -a option will print the size of each file in the directories, too

echo "hello" echo "hello" >> output.txt echo "The path variable is $PATH"

The echo command does one simple job: it prints to the output the argument passed to it. We can append the output to a file by >>. We can interpolate environment variables.

env

The env command can also be used to print out all the environment variables. If run with no options.

find . -name '*.js'

The find command can be used to find files or folders matching a particular search pattern. It searches recursively. Let's learn how to use it by example. Find all the files under the current tree that have the .js extension and print the relative path of each file that matches:

drwxr-xr-x

The first letter indicates the type of file: - means it's a normal file, d means it's a directory, l means it's a link. Then you have 3 sets of values: The first set represents the permissions of the owner of the file. The second set represents the permissions of the members of the group the file is associated to The third set represents the permissions of the everyone else

gunzip filename.gz gunzip -c filename.gz > anotherfilename

The gunzip command is basically equivalent to the gzip command, except the -d option is always enabled by default. You can extract to a different filename using output redirection using the -c option

less <filename>

The less command is one I use a lot. It shows you the content stored inside a file, in a nice and interactive UI

uname -mp

The m option shows the hardware name (x86_64 in this example) and the p option prints the processor architecture name (i386 in this example)

uname -n

The n option prints the node network name

open <filename>

The open command lets you open a file using this syntax

ping www.google.com

The ping command pings a specific network host, on the local network or on the Internet. Once ping is stopped, it will print some statistics about the results: the percentage of packages lost, and statistics about the network performance. As you can see the screen prints the host IP address, and the time that it took to get the response back. Not all servers support pinging, in case the request times out.

uname -srv

The s option prints the Operating System name. r prints the release, and v prints the version

clear clear -x

The screen will clear and you will just see the prompt at the top. Once you do that, you will lose access to scrolling to see the output of the previous commands entered. So you might want to use clear -x instead, which still clears the screen, but lets you go back to see the previous work by scrolling up

tar -cf archive.tar file1 file2

The tar command is used to create an archive, grouping multiple files in a single file. Its name comes from the past and means tape archive (back when archives were stored on tapes). This command creates an archive named archive.tar with the content of file1 and file2. The c option stands for create. The f option is used to write to file the archive.

top

The top command is used to display dynamic real-time information about running processes in the system

ls -al | wc 6 47 284

The wc command gives us useful information about a file or input it receives via pipes. Example via pipes, we can count the output of running the ls -al command. The first column returned is the number of lines. The second is the number of words. The third is the number of bytes.

who who -aH who am i

The who command displays the users logged in to the system. The -aH flags will tell who to display more information, including the idle time and the process ID of the terminal. The special who am i command will list the current terminal session details

man <command>

This is a man (from _manual_) page. Man pages are an essential tool to learn as a developer. They contain so much information that sometimes it's almost too much.

mkdir fruits cp -r fruits cars # will copy fruits folder into cars folder

To copy folders you need to add the -r option to recursively copy the whole folder contents. Will create new folder if non specified before.

rm -rf fruits cars

To delete folders with files in them, we'll use the more generic rm command which deletes files and folders, using the -rf option. Be careful as this command does not ask for confirmation and it will immediately remove anything you ask it to remove.

tar -xf archive.tar tar -xf archive.tar -C directory

To extract files from an archive in the current folder, use. the x option stands for extract. And to extract them to a specific directory use -C

pwd

To know which directory you are in, you can use the "pwd" command. It gives us the absolute path, which means the path that starts from the root. The root is the base of the Linux file system. It is denoted by a forward slash( / ).

tar -xf archive.tar.gz

To unarchive a gzipped archive, you can use gunzip, or gzip -d, and then unarchive it. But tar -xf will recognize it's a gzipped archive, and do it for you

whoami

Type whoami to print the user name currently logged in to the terminal session

ls -al ls /bin ls

Use the "ls" command to know what files are in the directory you are in. You can see all the hidden files by using the command "ls -a". -al means show data of all files including hidden ones

passwd

Users in Linux have a password assigned. You can change the password using the passwd command. When you're root (or have superuser privileges) you can set the username for which you want to change the password

echo * echo o* echo ~ echo $(ls -al) echo {1..5}

We can echo the files in the current folder. We can echo the files in the current folder that start with the letter o. You can print your home folder path using ~. You can also execute commands, and print the result to the standard output (or to file, as you saw). You can generate a list of strings, for example ranges.

wc -l test.txt wc -w test.txt wc -c test.txt wc -m test.txt

We can tell it to just count the lines, or just the words, or just the bytes. Bytes in ASCII charsets equate to characters. But with non-ASCII charsets, the number of characters might differ because some characters might take multiple bytes (for example this happens in Unicode). In this case the -m flag will help you get the correct value.

traceroute www.google.com

When you try to reach a host on the Internet, you go through your home router. Then you reach your ISP network, which in turn goes through its own upstream network router, and so on, until you finally reach the host. Have you ever wanted to know what steps your packets go through to do that? The traceroute command is made for this.

su <username>

While you're logged in to the terminal shell with one user, you might need to switch to another user. For example you're logged in as root to perform some maintenance, but then you want to switch to a user account. You can do so with the su command

mkdir -p fruits/apples

You can also create multiple nested folders by adding the -p option

rmdir fruits cars

You can also delete multiple folders at once (must be empty)

tar -tf archive.tar

You can also just list the files contained in an archive

env -u HOME node app.js

You can also make a variable inaccessible inside the program you run, using the -u option. For example this code removes the HOME variable from the command environment

open <directory name>

You can also open a directory, which on macOS opens the Finder app with the current directory open

env -i node app.js env -i /usr/local/bin/node app.js

You can also run the command clearing all the environment variables already set, using the -i option. Sometimes you need to pass the full path to the node program.

cd /etc

You can also use absolute paths, which start from the root folder /

printenv PATH # Print value of path

You can append a variable name as a parameter, to only show that variable value.

gzip filename gzip -c filename > filename.gz

You can compress a file using the gzip compression protocol named LZ77 using the gzip command. This will compress the file, and append a .gz extension to it. The original file is deleted. To prevent this, you can use the -c option and use output redirection to write the output to the filename.gz file.

gzip filename1 filename2 gzip -r a_folder

You can compress multiple files by listing them. You can compress all the files in a directory, recursively, using the -r option.

cp apple another_apple

You can copy a file using the cp command, makes new file if not exist

alias ll='ls -al' alias

You can create a new command, for example I like to call it ll, that is an alias to ls -al. Once you do, you can call ll just like it was a regular UNIX command. Now calling alias without any option will list the aliases defined. To make it permanent, you need to add it to the shell configuration. This could be ~/.bashrc or ~/.profile or ~/.bash_profile if you use the Bash shell, depending on the use case.

touch apple

You can create an empty file using the touch command

mkdir dogs cars

You can create multiple folders with one command

find folder1 folder2 -name filename.txt find . -type d -name node_modules -or -name public find . -type d -name '*.md' -not -path 'node_modules/*'. find . -type f -size +100c Search files bigger than 100KB but smaller than 1MB find . -type f -mtime +3 find . -type f -mtime -1 find . -type f -mtime -1 -delete find . -type f -exec cat {} \;

You can search under multiple root trees. Find directories under the current tree matching the name "node_modules" or 'public'. You can also exclude a path using -not -path. You can search files that have more than 100 characters (bytes) in them. Search files bigger than 100KB but smaller than 1MB. Search files edited more than 3 days ago +3 or Search files edited in the last 24 hours -1. You can delete all the files matching a search by adding the -delete option. This deletes all the files edited in the last 24 hours. You can execute a command on each result of the search. In this example we run cat to print the file content. Notice the terminating \;. {} is filled with the file name at execution time.

sudo -u flavio ls /Users/flavio

You can use sudo to run commands as any user. root is the default, but use the -u option to specify another user

cd ..

You can use the .. special path to indicate the parent folder. Go back one folder

mkdir fruits

You create folders using the mkdir command

diff -rq dir1 dir2

You must use the -r option to compare recursively (going into subdirectories). In case you're interested in which files differ, rather than the content, use the r and q options

chmod a+r filename #everyone can now read chmod a+rw filename #everyone can now read and write chmod o-rwx filename #others (not the owner, not in the same group of the file) cannot read, write or execute the file chmod og-r filename #other and group can't read any more

You type chmod followed by a space, and a letter: a stands for all u stands for user g stands for group o stands for others. Then you type either + or - to add a permission, or to remove it. Then you enter one or more permission symbols (r, w, x).

cd fruits

cd means change directory. You invoke it specifying a folder to move into. You can specify a folder name, or an entire path.

diff dogs.txt moredogs.txt

diff is a handy command. Suppose you have 2 files, which contain almost the same information, but you can't find the difference between the two. diff will process the files and will tell you what's the difference. Using the -y option will compare the 2 files line by line. Comparing directories works in the same way.

gzip -d filename.gz

gzip can also be used to decompress a file, using the -d option.

.filename

hidden files start with a .

.

indicates the current folder.

killall <name>

killall will send the signal to multiple processes at once instead of sending a signal to a specific process id. where name is the name of a program. For example you can have multiple instances of the top program running, and killall top will terminate them all.

sort | ls

sort does not just work on files, as many UNIX commands do - it also works with pipes. So you can use it on the output of another command. For example you can order the files returned by ls with.

sudo sudo -i

sudo is commonly used to run a command as root. You must be enabled to use sudo, and once you are, you can run commands as root by entering your user's password (not the root user password). You can run sudo -i to start a shell as root

tar -czf archive.tar.gz file1 file2

tar is often used to create a compressed archive, gzipping the archive. This is done using the z option. This is just like creating a tar archive, and then running gzip on it.

crontab -e EDITOR=nano crontab -e

to edit the cron jobs, and add new ones. By default this opens with the default editor, which is usually vim. I like nano more. You can use this line to use a different editor

dirname /Users/flavio/test.txt

will return the /Users/flavio string

basename /Users/flavio/test.txt

will return the text.txt string. If you run basename on a path string that points to a directory, you will get the last segment of the path. In this example, /Users/flavio is a directory, flavio is returned.

rmdir fruits

you can delete a folder using rmdir (must be empty)


Conjuntos de estudio relacionados

Chapter 6: Interest Groups and Lobbying

View Set

Module 4 Databases, Data Warehousing, and Data Mining Did I get this, Learn by Doing

View Set

Nutrition Ch 6: Protein Study Guide, NHCC

View Set

Fresenius ~ Water Treatment Study Guide

View Set