Linux

Kommandoer fra CodeAcademy kurset

ls – list contents (all files and directories) of the directory. (Similar to dir).
pwd - stands for “print working directory”.
cd - stands for “change directory”.
cd ../feb
cd ../../2014/feb

mkdir media - create a new directory in the working directory
touch keyboard.txt - create an empty file inside the working directory.

ls -a - lists all contents, including hidden files and directories
-l - lists all contents of a directory in long format
-t - order files and directories by the time they were last modified.

ls -alt - lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.

cp frida.txt lincoln.txt - cp command copies files or directories, we copy the contents of frida.txt into lincoln.txt.
cp * satire/ - wildcards *, copy all to satire.
cp m*.txt scifi/ - copy all text files starting with “m”.

mv superman.txt superhero/ - move file.
mv wonderwoman.txt batman.txt superhero/ - move several files.
mv batman.txt spiderman.txt - batman.txt has been renamed to spiderman.txt.

rm waterboy.txt - remove files.
rm -r slapstick - remove directory. The -r is an option that modifies the behavior of the rm command. The -r stands for “recursive,” and it’s used to delete a directory and all of its child directories.

echo "Hello" - print to the console.
echo "Hello" > hello.txt - write Hello to the file hello.txt. The > command redirects the standard output to a file.

cat hello.txt - open file.
cat oceans.txt > continents.txt - Here the standard output of cat oceans.txt is redirected to continents.txt. > overwrites all original content in continents.txt.

cat glaciers.txt >> rivers.txt - The standard output of the command on the left is appended (added) to the file on the right.

cat < lakes.txt - The < takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat command. The standard output appears in the terminal. (Similar to cat lakes.txt).

wc - outputs the number of lines, words, and characters in volcanoes.txt, respectively.

cat volcanoes.txt | wc - The | “pipe” takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “command to command” redirection. Here the output of cat volcanoes.txt is the standard input of wc.

cat volcanoes.txt | wc | cat > islands.txt - Multiple | can be chained together. Here the standard output of cat volcanoes.txt is “piped” to the wc command. The standard output of wc is then “piped” to cat. Finally, the standard output of cat is redirected to islands.txt.

sort lakes.txt - sorts contents alphabetically.
cat lakes.txt | sort > sorted-lakes.txt - Here, the command takes the standard output from cat lakes.txt and “pipes” it to sort. The standard output of sort is redirected to sorted-lakes.txt.

uniq deserts.txt - show only unique entries. Filters out adjacent, duplicate lines in a file.
sort deserts.txt | uniq - A more effective way to call uniq is to call sort to alphabetize a file, and “pipe” the standard output to uniq. Here by piping sort deserts.txt to uniq, all duplicate lines are alphabetized (and thereby made adjacent) and filtered out.

grep Mount mountains.txt - grep stands for “global regular expression print”. It searches files for lines that match a pattern. Here, grep searches for “Mount” in mountains.txt.

grep -i Mount mountains.txt - grep -i enables the command to be case insensitive. Here, grep searches for capital or lowercase strings that match Mount in mountains.txt.

grep -R Arctic /home/ccuser/workspace/geography - The grep can also be used to search within a directory. One can use regular expressions to search for patterns in files. -R stands for “recursive”.

grep -Rl Arctic /home/ccuser/workspace/geography - The grep -Rl searches all files in a directory and outputs only filenames with matched results. -R stands for “recursive” and l stands for “files with matches”.

sed 's/snow/rain/' forests.txt - The sed stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find and replace”.
s: stands for “substitution”. it is always used when using sed for substitution.
snow: the search string, the text to find.
rain: the replacement string, the text to add in place.

sed 's/snow/rain/g' forests.txt - The above command uses the g expression, meaning “global”. Here sed searches forests.txt for the word “snow” and replaces it with “rain”, globally. All instances of “snow” on a line will be turned to “rain”.

nano hello.txt - open the nano text editor. Ctrl + O (the letter, not the number) to save the file. Press Enter. Ctrl + X exits the nano program. ‘X’ stands for exit.

nano ~/.bash_profile - The ~ represents the user’s home directory. The . indicates a hidden file. ~/.bash_profile is the name of file used to store environment settings.

source ~/.bash_profile - activates the changes in ~/.bash_profile for the current session. Instead of closing the terminal and needing to start a new session, source makes the changes available right away in the session we are in.

alias pd="pwd" - The alias command allows you to create keyboard shortcuts, or aliases, for commonly used commands. Here alias pd="pwd" creates the alias pd for the pwd command, which is then saved in the bash profile. Each time you enter pd, the output will be the same as the pwd command.

Start IT systemer på SDU Kommandolinjen Fjernadgang