http://www.techradar.com/us/how-to/computing/how-to-master-the-linux-terminal-with-core-commands-1305479
There are thousands of terminal commands, from the commonplace to the
arcane, but you need only a handful of key commands to get started in
Linux.
Here we will look at some of the core workhorse commands, giving a brief description of what each one is for. As always, the man pages give far more detail on how to use them.
Many of these produce more output than can fit in your terminal display, so consider piping them through Less (as described in our introduction to apt-get).
nano -w somefile.txt
The -w option turns off word wrapping, which you certainly don't want when editing configuration files. The status bar at the bottom shows the main commands – for example, press Ctrl + X to save your file and exit.
This assumes you know which file you want, but what if you know what you're looking for but not the name of the file? In that case, use grep. This searches text files for a string or regular expression.
grep sometext *.txt
This will search all .txt files in the current directory and show any lines containing the matching text from each file, along with the name of the file. You can even search an entire directory hierarchy with -r (or --recursive):
grep -r -I sometext somedir
Be careful when you're searching large directory trees: it can be slow and return strange results from any non-text files it searches. The -I option tells grep to skip such binary files.
Text is also the preferred way of passing data between many programs, using the pipes we looked at previously. Sometimes you want to pass data straight from one program to the next, but other times you may want to modify it first. You could send the text to a file, edit it and then send the new file to the next program, or you could pass it though a pipe and modify it on-the-fly.
Nano edits files interactively, grep searches them automatically, so we just need a program to edit automatically; it's called sed (Stream EDitor). Sed takes a stream of text, either from a file or a pipe, and makes the changes you tell it to. The most common uses are deletion and substitution. Normally, sed sends its output to stdout, but the -i option modifies files in place:
sed -i 's/oldtext/newtext/g' somefile.txt
sed -i '/oldtext/d' somefile.txt
The second example deletes all lines containing "oldtext".
Another useful program is awk, which can be used to print specific items from a text file or stream.
awk '{print $1}' somefile.txt
cat *.txt | awk '/^Hello/ {print $2}'
The first example prints the first word from each line of the file. The second takes the contents of all files ending in .txt, filters the lines starting with "Hello" (the string between the slashes is a pattern to match) and then prints the second word from each matching line.
Discover the core commands for file and text handling and networking
Here we will look at some of the core workhorse commands, giving a brief description of what each one is for. As always, the man pages give far more detail on how to use them.
Many of these produce more output than can fit in your terminal display, so consider piping them through Less (as described in our introduction to apt-get).
File handling
Central to any terminal activity is working with files, creating, removing, listing and otherwise examining them. Here are the main commands for this.- ls: lists the contents of the current or given directory.
- ls -l: as for ls, but gives more information about each item. Add human-readable file sizes with -h
ls -lh MyPhotos - rm: deletes a file. Use the -i option for confirmation before each removal or -f to blitz everything. With -r it deletes directories and their contents too.
- rmdir: deletes a directory, which must be empty.
- df: Shows free disk space on all filesystems or just those given on the command line.
- du: Shows the amount of space used by individual files or directories.
df -h /home
du -sh /home/user/* - file: identifies the type of a file. Unlike Windows, which uses the file name extension, this command looks inside the file to see what it really contains.
- find: searches the current or given directory for files matching certain criteria. For example you could find all LibreOffice spreadsheets with
find Documents -name '*.ods' - locate: This also looks for files but using a much faster system. The locate database is automatically rebuilt each day by updatedb, and locate then searches this. It's fast, but doesn't know about very recent changes.
Text handling
Text files are all around us, from emails to configuration files, and there are plenty of commands to deal with them. If you want to edit a text file, there are a number of choices, with the two big ones being Emacs and Vi. Both are overkill if you just want to tweak a configuration file; in this instance, try nano instead:nano -w somefile.txt
The -w option turns off word wrapping, which you certainly don't want when editing configuration files. The status bar at the bottom shows the main commands – for example, press Ctrl + X to save your file and exit.
This assumes you know which file you want, but what if you know what you're looking for but not the name of the file? In that case, use grep. This searches text files for a string or regular expression.
grep sometext *.txt
This will search all .txt files in the current directory and show any lines containing the matching text from each file, along with the name of the file. You can even search an entire directory hierarchy with -r (or --recursive):
grep -r -I sometext somedir
Be careful when you're searching large directory trees: it can be slow and return strange results from any non-text files it searches. The -I option tells grep to skip such binary files.
Text is also the preferred way of passing data between many programs, using the pipes we looked at previously. Sometimes you want to pass data straight from one program to the next, but other times you may want to modify it first. You could send the text to a file, edit it and then send the new file to the next program, or you could pass it though a pipe and modify it on-the-fly.
Nano edits files interactively, grep searches them automatically, so we just need a program to edit automatically; it's called sed (Stream EDitor). Sed takes a stream of text, either from a file or a pipe, and makes the changes you tell it to. The most common uses are deletion and substitution. Normally, sed sends its output to stdout, but the -i option modifies files in place:
sed -i 's/oldtext/newtext/g' somefile.txt
sed -i '/oldtext/d' somefile.txt
The second example deletes all lines containing "oldtext".
Another useful program is awk, which can be used to print specific items from a text file or stream.
awk '{print $1}' somefile.txt
cat *.txt | awk '/^Hello/ {print $2}'
The first example prints the first word from each line of the file. The second takes the contents of all files ending in .txt, filters the lines starting with "Hello" (the string between the slashes is a pattern to match) and then prints the second word from each matching line.
Networking
We normally picture big graphical programs like Chromium and Thunderbird when we think of networked software, but there are many command line programs for setting up, testing and using your network or internet connection.- Ping sends a small packet to a
remote server and times the response, which is useful if you want to
check whether a site is available, or if your network is working.
ping -c 5 www.google.com - wget: downloads files. The only argument it needs is a URL, although it has a huge range of options that you will not normally need.
- hostname: shows your computer's host name, or its IP address with -i.
- lynx: a text mode web browser. While not as intuitive as Chromium or Firefox, it is worth knowing about in case you ever suffer graphics problems.
No comments:
Post a Comment