1. What is Linux?
Linux is a free and open-source operating system. It is based on Unix ideas and it runs on many kinds of hardware.
Full lesson: Linux tutorial
45 questions about Linux with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.
Linux is a free and open-source operating system. It is based on Unix ideas and it runs on many kinds of hardware.
Full lesson: Linux tutorial
Linus Torvalds wrote the first Linux kernel in 1991. He wrote it as a hobby while he studied at the University of Helsinki.
Full lesson: Linux intro
A Linux distribution is a package of the Linux kernel and application software. The distribution gives you a ready system to install and use.
Full lesson: Linux intro
The shell is a program that gives you an interface to the kernel. It is both a command language and a programming language.
Full lesson: Linux shell
Write the variable name, then an equals sign, then the value. Do not put spaces around the equals sign.
your_name="example"
Full lesson: Linux shell variable
First make the script executable. Then run it with its path.
chmod +x ./test.sh
./test.sh
Full lesson: Linux shell
Give echo the text that you want to print. The shell writes the text to the screen.
echo "Hello Linux"
Full lesson: Linux shell echo
Put the values in parentheses and separate them with spaces. Array subscripts start from 0.
my_array=(A B "C" D)
Full lesson: Linux shell array
Use the type command with the command name. It tells you if the command is a built-in, an alias, a function, or an external program.
type ls
Full lesson: Linux comm type
Use the diff command with the two file names. It compares the files line by line and shows the differences.
diff file1.txt file2.txt
Full lesson: Linux comm diff
Use the file command with the file name. It prints the file type.
file report.txt
Full lesson: Linux comm file
Use the df command. The -h option shows the sizes in a human-readable form.
df -h
Full lesson: Linux filesystem
Use the ifup command with the interface name. It reads the configuration file and sets the network parameters.
sudo ifup eth0
Full lesson: Linux comm ifup
Use the diff3 command with the three file names. It shows the differences among all three files.
diff3 my.txt orig.txt other.txt
Full lesson: Linux comm diff3
The shell reads the spaces around the equals sign as separate words. Write the assignment with no spaces.
var="value"
Full lesson: Linux shell variable
Put a dollar sign before the variable name. You can also wrap the name in braces.
name="Linux"
echo $name
echo ${name}
Full lesson: Linux shell variable
Use the mtype command with the file name. The -t option converts MS-DOS line endings to Linux line endings.
mtype -t dos.txt
Full lesson: Linux comm mtype
Write the function name, then a pair of parentheses, then a block in braces. You can add the function keyword before the name.
greet() {
echo "Hello"
}
greet
Full lesson: Linux shell func
Use the test command or a pair of square brackets. Put spaces inside the brackets.
if [ -f file.txt ]; then
echo "File exists"
fi
Full lesson: Linux shell test
Use the printf command with a format string. printf does not add a newline by itself.
printf "Name: %s\n" "Linux"
Full lesson: Linux shell printf
Use the ifdown command with the interface name. The command reads the interface settings from the configuration file.
ifdown eth0
Full lesson: Linux comm ifdown
Use the ifconfig command with the device name. Run it without arguments to show all active devices.
ifconfig eth0
Full lesson: Linux comm ifconfig
Use the install command with the source file and the target file. Add options to set the mode and the owner.
install -m 755 app.sh /usr/local/bin/app.sh
Full lesson: Linux comm install
Pipe the diff output to the diffstat command. diffstat counts the inserted and deleted lines for each file.
diff old.txt new.txt | diffstat
Full lesson: Linux comm diffstat
Use the source command or a dot before the file name. The included file runs in the current shell.
. ./config.sh
Full lesson: Linux shell include file
Put the expression inside double parentheses for arithmetic. Use the test command for string and number comparisons.
a=5
b=3
if [ $a -gt $b ]; then
echo "a is larger"
fi
Full lesson: Linux shell basic operators
Run yum install with the package name. Add the -y option to answer yes to all prompts.
yum install -y nginx
Full lesson: Linux yum
A function returns the exit status of its last command when you omit the return statement. Add return with a number from 0 to 255 to set the value.
myfunc() {
echo "done"
return 0
}
Full lesson: Linux shell func
The square brackets need a space after the opening bracket and before the closing bracket. A missing space makes the shell read the bracket as part of a word.
[ -f file.txt ] && echo "ok"
Full lesson: Linux shell test
printf does not add a newline at the end of the output. Add the \n escape sequence to the format string.
printf "line one\nline two\n"
Full lesson: Linux shell printf
Use the vi command followed by the file name. The editor opens the file in command mode.
vi example.txt
Full lesson: Linux vim
Run the w command. It lists the logged-in users and their current programs.
w
Full lesson: Linux comm w
Give the user name to the w command. The command shows data for that user only.
w alice
Full lesson: Linux comm w
First install the build tools with yum. Then download and compile the MySQL source code.
yum -y install gcc gcc-c++ make autoconf
Full lesson: Mysql install setup
First install the build tools and libraries with yum. Then compile Nginx from source.
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Full lesson: Nginx install setup
Open the command library page. It groups commands by category for easy search.
Full lesson: Linux command manual
Use the expr command inside backticks. Assign the result to a variable.
val=`expr 2 + 2`
echo "The sum is: $val"
Full lesson: Linux shell basic operators
Use a relational operator with the test command. The -eq operator checks for equality.
if [ $a -eq $b ]; then echo equal; fi
Full lesson: Linux shell basic operators
Use the -d file test operator with the test command. It returns true if the path is a directory.
if [ -d /tmp ]; then echo yes; fi
Full lesson: Linux shell test
Use the -z string test operator. It returns true when the string has zero length.
if [ -z "$name" ]; then echo empty; fi
Full lesson: Linux shell test
The cd command runs in a subshell. The parent shell does not change its directory.
Full lesson: Linux shell
The variable var is not set in the current shell. Set the variable before you use it.
var=hello
echo $var
Full lesson: Linux shell variable
The empty value leaves the test with too few arguments. Quote the variables to keep the test valid.
if [ "$a" = "$b" ]; then echo same; fi
Full lesson: Linux shell test
The file lib.sh is not in the current directory. Use the correct path or run the command from the right directory.
source ./scripts/lib.sh
Full lesson: Linux shell include file
The package needs other packages that are not installed. Use yum to resolve the dependencies automatically.
yum install package_name
Full lesson: Linux yum