PHY224 Programmieren in C++ - Linux Befehle

Common Linux Commands

There are of course thousands of Linux related commands and procedures. I will give a few of the more common ones here but I assure you there is a lot more to Linux than just these relatively simple commands. A few tips with Linux type man command to get an extensive help file. For example type
      man ls
to get the manual page for the ls command containing information on the various switches and uses of the ls command.

You can also execute multiple commands by separating each one with a ``;''. For example
      cd newdir; mkdir thatdir; ls -la
will first change directories to the newdir directory, then create a directory called thatdir, then list all the files in long format. You can string together as many commands as you like but caution should be used not to inadvertently do anything harmful.

Contents

cd
change directory
chmod
change the access permissions of files
cp
copy files
df
summarize free disk space
du
summarize disk usage
find
search for files in a directory hierarchy
head
output the first part of files
kill
terminate a process
less
opposite of more, file perusal filter for crt viewing
ls
list contents of directories
mkdir
make directories
more
file perusal filter for crt viewing
mv
rename files
ps
report process status
pwd
print name of current/working directory
rm
remove files
rmdir
remove empty directories
tail
output the last part of files
top
display CPU processes
Editors	 

vi
text editor
kate
advanced text editor for KDE
nedit
GUI style editor for plain text files
emacs
extensible, customizable, self-documenting real-timedisplay editor
 

cd

This command is used to change the directory and using this command will change your location to what ever directory you specify
      cd hello
will change to the directory named hello located inside the current directory
      cd /home/games
will change to the directory called games within the home directory. As you can see you can specify any directory on the Linux system and change to that directory from any other directory. There are of course a variety of switches associated with the cd command but generally it is used pretty much as it is.
Type man cd for more information on the cd command. See also ls.

chmod

When you perform a listing using the -l switch you will see all kinds of letters and - signs before the individual files and directories.

  -rwxr--r--     1 root     root       1024 Jan 27 09:45 happy 
  lrwxrwxrwx     1 root     root       1024 Feb 13 09:45 myfile3 
  drwxr-xr--     1 root     root       1024 Feb 18 09:45 logs

What does it all mean?
Well the first 10 letters that look similar to this lrwxrwxrwx contain the permissions that can be controlled with the chmod command (the first letter you cannot change as you will see for obvious reasons). The first letter (lrwxrwxrwx) is simply the type of file it is either a l for a link to another file, d for a directory or - for a file and is set by the Linux operating system you can not manually change this letter (unless you change the file type of course). Next come the permissions for the owner (-rwxrwxrwx) followed by the permissions for the group (-rwxrwxrwx) and permissions for others (-rwxrwxrwx). The three letters stand for read, write and executable. If the letter is present then that particular permission is on and if there is a - in its place it is off. This means the owner of a file can alter and change the permissions to what ever they like for example -rwxr-r- means that the file is readable, writable and executable for the owner but only readable for everyone else. Another example would be -rwxr-x-x this would mean that the file is readable, writable and executable by the owner but only readable and executable for everyone in the group and only executable to others outside the group. As you can imagine this ability to give or remove permissions can be very handy and adds to the security of Linux. To change permissions on a file you need to be the owner and you can do it in two ways. The format for the chmod command is
      chmod xyz file
with x representing the permissions for the owner followed by y the permissions for the group then z the permissions for all others. The file is the name of the relevant file being modified. You can use a variety of methods to set the permissions for your files. The first is by using octal encoding where by numbers represent the
rwx     letters:       Read = 4, Write = 2, Execute =1.
So if you wanted to set up the permission for the file myfile2 to be rwxrw-r- you will need to add up the numbers for the owner being rwx (4+2+1=7), the group being rw (4+2=6) and others r (2) so the chmod command will be
      chmod 762 myfile2
Though this may seem confusing at first you will learn it quite easily with practice and it will prove to be virtually fool proof over time. You can also add permissions
      chmod +x file
This will make the file executable to all users. You can also use the following
      chmod go-rwx file
This will remove all permissions from the group and other users making the file only readable, writable or executable by the owner and no one else.
      chmod o+x file
This will add the executable permission to other users only. You can also apply permissions to directory meaning you can restrict who can list the directory contents. You can allow them to view files in the directory but not list the directory contents. This and other permissions enable the owner of a file or directory to have complete control over its use enabling more security and privacy.

cp

The cp command copies files. You can copy a file in within the current directory or you can copy files to another directory.
      cp myfile.html /home/help/mynewname.html
This will copy the file called myfile.html in the current directory to the directory /home/help/ and call it mynewname.html. Simply put the cp command has the format of
      cp file1 file2
with file1 being the name (including the path if needed) of the file being copied and file2 is the name (including the path if needed) of the new file being created. Remember with the cp command the original file remains in place.
Type man cp to see more about the cp command.

df

The disk free command shows how much memory is being used and how much is free for every partition and mounted file system (including any Window drives).
Type man df for more information about the df command.

du

The disk usage command shows how much memory is being used by each directory below that from which the command was given. If du is run from the root directory it will show the memory used by every directory on the system, including any mounted file systems (including other drives) such as any Windows related drives.
Type man du for more information about the du command.

find

The find command is used to find files and or folders within a Linux system.

To find a file using the find command you type
      find /usr/bin -name filename
this will search inside the /usr/bin directory (and any sub directories within the /usr/bin directory) for the file named filename. To search the entire filing system including any mounted drives use
      find / -name filename
and the find command will search every file system beginning in the root directory. The find command can also be used to find command to find files by date and the find command happily understand wild characters such as * and ?.
Type man find for more information on the find command.

head

The head command list the first lines of a file. By default it will display the first ten lines of a file. For example
      head filename
will list the first ten lines of the file named filename. You can also select how many lines to show, for example
      head -5 filename
will list the first 5 lines of the file named filename. The format for the head command is
      head -n filename
with the number of lines to be displayed being n and the file name of the file, including the path if needed, you wish to view being in place of filename.
Type man head for more information on the head command.

kill

The kill command is used to kill a process by using the associated PID (Process ID) number
      kill 381
This will kill the process with the PID of 381. Be careful using the kill command because it is easy to accidently kill an important process. To see the current list of processes that are running use the ps command. Typing ps au will display every process that is in operation including background processes and those being conducted by other users.
See also ps and top.

less

This command allows you to scroll through a file a page at a time. The less command is very similar to the more command only it is more advanced and has more features associated with it.
      less filename
Type man less for more information on the less command.

ls

The ls command lists the contents of a directory. In its simple form typing just ls at the command prompt will give a listing for the directory you are currently in. The ls command can also give listings of other directories without having to go to those directories for example typing
      ls /dev/bin
will display the listing for the directory /dev/bin. The ls command can also be used to list specific files by typing ls filename this will display the file filename (of course you can use any file name here). The ls command can also handle wild characters such as the * and ?. For example
      ls a*
will list all files starting with lower case a
      ls [aA]*
will list files starting with either lower or upper case a (remember Linux is case sensitive) or
      ls a?
will list all two character file names beginning with lower case a. There are many switches (over 70) associated with the ls command that perform specific functions. Some of the more common switches are listed here.

 

  • ls -a   This will list all file including those beginning with the ``.'' that would normally be hidden from view.
  • ls -l   This gives a long listing showing file attributes and file permissions.
  • ls -s   Will display the listing showing the size of each file rounded up to the nearest kilobyte.
  • ls -S   This will list the files according to file size.
  • ls -C   Gives the listing display in columns.
  • ls -F   Gives a symbol next to each file in the listing showing the file type. The / means it is a directory, the * means an executable file, the @ means a symbolic link.
  • ls -r   Gives the listing in reverse order.
  • ls -R   This gives a recursive listing of all directories below that where the command was issued.
  • ls -t   Lists the directory according to time stamps.

Switches can be combined to produce any output you desire.
      ls -la
This will list all the files in long format showing full file details.
Type man ls for more details about the ls command. See also cd.

mkdir

The mkdir command is used to create a new directory.
      mkdir mydir
This will make a directory (actually a sub directory) within the current directory called mydir.
Type man mkdir to see more about the mkdir command.

more

This command allows you too scroll through a file one screen at a time allowing you to more easily read the files contents. Some files are very big and using this command allows you to view the contents of large files more efficiently. To go forward one screen use the space bar and to go back one screen use the B key.
      more filename
Type man more for more information about the more command.

mv

The mv command moves files from one location to another. With the mv command the file will be moved an no longer exist in its former location prior to the mv. The mv command can also be used to rename files. You can move files within the current directory or another directory.
      cp myfile.html /home/help/mynewname.html
This will move the file called myfile.html in the current directory to the directory /home/help/ and call it mynewname.html. The mv command has the format of
      mv file1 file2
with file1 being the name (including the path if needed) of the file being moved and file2 is the name (including the path if needed) of the new file being created.
Type man mv to see more about the mv command.

ps

The ps (process status) will by default only show the processes that you as a user have started. However Linux is always running background tasks so you may want to use some of the common switches associated with the ps such as ps au to display the processes running for all users and in the user format hence we get to see every process that is running on the system. When a process is started it is given among other things a Process ID (PID) number that is unique to it. This PID number can be seen by using the ps command or top command. By knowing a Process ID number you may opt to kill the process if you choose.
See also kill and top.

pwd

The pwd command (print working directory) will display the current directory. Typing
      pwd
will display something similar to this /home/games/help being the details of the current directory.
Type man pwd to get more information about the pwd command.

rm

The rm command is used to delete files. Some very powerful switches can be used with the rm command so be sure to check the man rm file before placing extra switches on the rm command.
      rm myfile
This will delete the file called myfile. You can include a path to delete a file in another directory for example
      rm /home/hello/goodbye.html
will delete the file named goodbye.html in the directory /home/hello/.

Some of the common switches for the rm command are

  • rm -i   This operates the rm command in interactive mode meaning it will prompt you before deleting a file. This gives you a second chance to say no do not delete the file or yes delete the file. Remember Linux is merciless and once something is deleted it is gone for good so the -i flag (switch) is a good one to get into the habit of using.
  • rm -f   Will force bypassing any safeguards that may be in place such as prompting. Again this command is handy to know but care should be taken with its use.
  • rm -r   Will delete every file and sub directory below that in which the command was given. Be very careful with this command as no prompt will be given in most Linux systems and it will mean instant good bye to your files if misused.

Type man rm to see more about the rm command.

rmdir

The rmdir command is used to delete a directory.
      rmdir mydir
This will delete the directory (actually a sub directory) called mydir.
Type man rmdir to see more about the rmdir command.

tail

The tail command list the last lines of a file. By default it will display the last ten lines of a file. For example
      tail filename
will list the last ten lines of the file named filename. You can also select how many lines to show for example
      tail -5 filename
will list the last 5 lines of the file named filename. The format for the tail command is
      tail -n filename
with the number of lines to be displayed being n and the file name of the file you wish to view, including the path if needed, being in place of filename.
Type man tail for more information on the tail command.

top

Typing top at the command prompt will activate the top application. Top simply lists all the operations in progress showing memory usage by each process. You have the option to kill any process if you want to but be careful if you kill a vital system you will run into trouble and may at the very least have to reboot to fix it.

Typically top is run in a spare xterm or X11 window while doing other tasks. To access the help in top use the ? or H key. To kill a task use the K key or change the priority of the task by using the R key.
Type man top to see more detailed information about the top command.

Editors

vi

The vi command is actually a text editor that comes as standard with most Linux packages.
Type man vi for more information on vi.

kate

kate is a multi-document editor part of KDE since release 2.2. Being a KDE application, kate ships with network transparency, as well as integration with the outstanding features of KDE. Choose it for viewing HTML sources from konqueror, editing configuration files, writing new applications or any other text editing task. You still need just one running instance of kate.

With a multi-view editor like kate you get a lot of advantages. You can view several instances of the same document and all instances are synchronized. Or you can view more files at the same time for easy reference or simultaneous editing.

Type man kate to see more about the kate command.

nedit

NEdit is a plain text editor with a graphical user interface mit pulldown menus. It is written to maximize usability for people who do intensive and continuous editing, but at the same time is easy to learn and to use casually. For programmers, NEdit provides automatic indent, block indentation adjustment, parenthesis flashing, and Unix ctags processing.
Type man nedit for more information on nedit.

emacs

GNU Emacs is a free, portable, extensible text editor. That it is free means specifically that the source code is freely copyable and redistributable. That it is portable means that it runs on many machines under many different operating systems, so that you can probably count on being able to use the same editor no matter what machine you are using. That it is extensible means that you can not only customize all aspects of its usage (from key bindings through fonts, colors, windows, mousage and menus), but you can program Emacs to do entirely new things that its designers never thought of.

Because of all this, Emacs is an extremely successful program, and does more for you than any other editor. It is particularly good for programmers. If you use a common programming language, Emacs probably provides a mode that makes it especially easy to edit code in that language, providing context sensitive indentation and layout. It also probably allows you to compile your programs inside Emacs, with links from error messages to source code; debug your programs inside Emacs, with links to the source; interact directly with the language interpretor (where appropriate); manage change logs; jump directly to a location in the source by symbol (function or variable name); and interact with your revision control system.

Emacs also provides mail readers, news readers, World Wide Web, gopher, FTP clients and spell checking, all of which are also useful for programming.
Type man emacs or info emacs for more information on emacs.