LPI Linux Essentials - Commands

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

kill / xkill / pkill / killall

All of these serve to "kill" a process, ie terminate it. The difference is what they accept as input. Kill wants the process ID, xkill allows you to click a window to close it, while killall and pkill accept the name of a process, but have somewhat different options and subtly different behavior. Note these do not belong to the same package, and xkill especially is not likely to be installed by default. We advise you to rectify that for your own convenience.

cd

Allows you to go from current directory to specified directory. Calling it without arguments returns you to your home directory. Calling it with two dots (cd ..) returns you to a directory "above" the current one, while calling it with a dash (cd -) returns you to the previous directory, regardless of where it's located relative to the current one.

head / tail

Another pair, but here both halves have their uses. Head outputs a number of the first ("head") lines of a file, while tail outputs a number of last ("tail") lines of a file. The default number is ten, but this can be controlled via the -n option. Another useful switch is -f, which is short for "follow", which continually outputs any appended lines - so, for instance, if you wanted to monitor a log file instead of constantly opening and closing it, you could use "tail -f /path/to/logfile".

ps / pgrep

As mentioned, kill needs the process ID. One way to obtain this is by using ps, which prints information about the currently active processes. The default output is not hugely useful, so stick an -e there to see information about every process on the system. This is only a snapshot, it will not update, see top for that. The pgrep command works in the following manner: you give it a process name, it gives you the process ID. Partial matches count, so be careful.

ls

By default, list the contents of the current directory. If you provide it a path, it will list the contents of that. Useful options to know are -l and -a, a long list format with more information and show hidden (dot) files, respectively.

chown

Change file ownership. Only the root user may change the owner of a file. To recursively change the owner for all the files in a directory, use it with -R.

chmod

Change user permissions. This refers to viewing, writing and executing files. A normal user may change permissions for files he owns.

dd

Convert and copy a file, according to its manpage. Not exactly the clearest or most useful description around, and yet, that's all dd does. You give it a source and a destination, and optionally some other commands, and it copies from one to the other. Its power comes from the flexibility - you can tell it the exact block size, it can copy around corrupted data, and it isn't picky about devices - if you want to overwrite your hard drive with zeroes straight from /dev/zero, you're welcome to do it. It's also commonly used for creating live USB sticks from hybrid ISO images.

cp

Copies files and directories. Since it doesn't copy directories recursively by default, remember to use -r or -a. The latter preserves mode, ownership and time stamp info in addition to recursively copying.

mkdir

Create new directories. The most handy switch is -p, which creates the entire specified structure if it doesn't exist already.

ln

Creates hard or symbolic links between files. Symbolic or soft links are sort of like Windows shortcuts, they provide a convenient way of accessing a particular file, though the analogy doesn't quite hold - symlinks can point to anything, but do not feature any metadata. You aren't very likely to ever use hard links, but knowing they're aliases to files - as opposed to symlinks, which are aliases to file names - can't hurt.

rm

Delete files and directories. Certainly a very useful command to know, as you cannot remove clutter without it. However, beware when using it. Although nowadays you'd really have to work on it to cause some damage to the system, you can still damage yourself - rm doesn't remove files to some imaginary wastebasket from which you can fish them out later when you realize you've made a horrible mistake, and "rm ate my homework" isn't going to convince anyone. Deleting directories requires recursive operation, so once again we have the -r switch.

grep

Grep, like all good Unix tools, does one thing, but does it well. It searches text for patterns. By default it looks at standard input, but you can specify files to be searched. A pattern can be a normal string or a regular expression. It can print out matching or non-matching lines, and their context. Every time you run a command which spews a lot of information you don't need, pipe it into grep and let it do its magic.

cat

If given a single file, prints its contents to the standard output. If you give it more than one file, it will concatenate them, and you can then redirect the output into a new file. Potentially useful is the -n option, which numbers the lines.

mv

Moves or renames files and directories. Essentially, moving and renaming is one operation - renaming is just "moving" a single file to the same place under a different name.

uname

Outputs some basic system information. By itself, it won't give you anything very useful ("Linux"), but call it with -a, and it will provide kernel information, as well as tell you the hostname and processor architecture.

pwd

Prints your current directory. Useful if your prompt doesn't contain this information, and especially useful in BASH programming for obtaining a reference to the directory in which you're executing the code.

find

Search the filesystem for files or directories. Find is a very versatile and powerful command, not only because of its searching capabilities, but also because it allows you to execute arbitrary commands on matching (or non-matching, even) files.

df

Show disk usage. The default output is good enough - it lists every filesystem, reports its size and the amount of used and available space - but you might want to tack on -h, which once again provides a more human-friendly report.

du

Show file or directory size. Among the more useful options are -h, which converts the reported sizes into a more human-friendly format, -s which gives only a summary instead of the whole listing, and -d which controls the depth of directory recursion.

diff

Shows the difference between two files via line by line comparison. It only shows altered lines, abbreviating changed as c, deleted as d and added as a.

sort

Sorts lines of text by various criteria. Among the more useful, there's -n, which sorts by the numeric value of a string, and -r, which reverses the output. An example of where this might come in handy is sorting du output - for example, if you wanted to see the files sorted in descending order according to size, you'd combine the two options.

su / sudo

Su and sudo are two ways of accomplishing the same thing - running a command as another user. The difference is that su switches you to a different user, while sudo only runs the command with another user's privileges.

uptime

Tells you how long has the system been running.

file

Tells you the type of a file. Since files in Linux aren't under obligation to have extensions for the system to work (not that having extensions always helps), sometimes it's hard for the user to know what type of file something is, and this little utility solves that problem.

whereis

Tells you where an executable binary files lives, provided it's in your path. It can also find its manual page and source code, provided they are present.

wc

The command line word counting utility. And line counting. And byte counting. And character counting.

ip

The ip command, which replaces ipconfig, netstat, route, etc. You could view it as a Swiss Army knife of networking.

man / whatis

The man command brings up the manual for a particular command. Most command line applications come with a man page. Whatis provides a one line summary lifted from the relevant sections of the manual. What are sections of the manual? See for yourself with "man man".

ping

The ping utility is a useful diagnostic tool. It allows you to quickly test if you're connected to your router or the Internet, and gives some indication of the quality of that connection. Pings are ICMP ECHO_REQUEST datagrams.

sleep

The sleep command is used to delay for a specified amount of time. You can use it to shutdown the computer after a certain period of time, or even as a makeshift alarm.

useradd, userdel, usermod

These commands allow you do add, delete and modify user accounts.

top / htop

These two are similar, both display processes, and can be thought of as console system monitors. We recommend you install htop the first chance you get if your distribution doesn't ship it by default, as it's a much improved version of top. For starters, it's not merely a viewer - it allows you to control processes via its user-friendly console GUI interface.

more / less

These two similar utilities allow you to view text chunked into screenfuls. Imagine a very long output from some command. Perhaps you called cat on a file and your terminal emulator took a few seconds to scroll all the text. Well, if you pipe it into one of these, you can now scroll it at your leisure. Less is newer and offers more options, so there is no reason to use more.

passwd

This command enables you to change your user account password. As root, you can reset normal user passwords, though you cannot view them. It's a good security practice to change your password every so often.

alias

This commands creates or changes aliases to other commands. What this means is, you can give names to new commands (or groupings of commands) or "rename" existing ones. It's very handy for abbreviating long strings of commands you find yourself using often, or giving more memorable names to things you don't use that often and have problems memorizing.

mount / umount

This pair takes care of mounting and unmounting filesystems. This can range from USB sticks to ISO images. Usually only root has mounting privileges.

time

Time a process. Think of it as a stopwatch for program execution. Contrary to what you might expect based on the name, it doesn't tell you the time. See date for that.

locate

Unlike find, locate searches the updated database for file name patterns. This database contains a snapshot of the filesystem. This makes locate very fast, but also unreliable - it cannot tell whether anything changed since the last snapshot.

date

Unlike time, date does exactly what you'd expect it - it prints out the date (and time) to the standard output. The output itself can be formatted to your specification, and it takes everything from the usual stuff like year, month, day, 12 or 24 hour format to nanoseconds and the ISO week number. For example, date +"%j %V" would give you the day of the year followed by the ISO week number.


Set pelajaran terkait

HISTORY & THEMES VIDEO QUIZ: The Pyramids of Giza

View Set

Chapter 13, users , groups and permissions 1002

View Set

Biochemistry CH4: Carbohydrate structure + function

View Set