Unix Linux Introduction
Unix Linux Introduction
Copyright 2009, Free Electrons. Creative Commons BY-SA 3.0 license Latest update: Jul 15, 2010, Document sources, updates and translations: http://free-electrons.com/docs/command-line Corrections, suggestions, contributions and translations are welcome!
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
3
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
4
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
5
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Text editors Compressing and archiving Printing files Comparing files and directories Looking for files Getting information about users
6
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Unix filesystem
7
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Everything is a file
Almost everything in Unix is a file! Regular files Directories Directories are just files listing a set of files Symbolic links Files referring to the name of another file Devices and peripherals Read and write from devices as with regular files Pipes Used to cascade programs cat *.log | grep error Sockets Inter process communication
8
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
File names
File name features since the beginning of Unix Case sensitive No obvious length limit Can contain any character (including whitespace, except /). File types stored in the file (magic numbers). File name extensions not needed and not interpreted. Just used for user convenience. File name examples: README .bashrc index.htm index.html Windows Buglist index.html.old
9
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
File paths
A path is a sequence of nested directories with a file or directory at the end, separated by the / character Relative path: documents/fun/microsoft_jokes.html Relative to the current directory Absolute path: /home/bill/bugs/crash9402031614568 / : root directory. Start of absolute paths for all files on the system (even for files on removable devices or network shared).
10
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
11
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
The Unix filesystem structure is defined by the Filesystem Hierarchy Standard (FHS): http://www.pathname.com/fhs/
13
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
14
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
15
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
16
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
17
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
ls command
Lists the files in the current directory, in alphanumeric order, except files starting with the . character. ls -a (all) Lists all the files (including .* files) ls -l (long) Long listing (type, date, size, owner, permissions) ls -t (time) Lists the most recent files first ls -S (size) Lists the biggest files first ls -r (reverse) Reverses the sort order ls -ltr (options can be combined) Long listing, most recent files at the end
18
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
19
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
20
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
21
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
22
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
The cp command
cp <source_file> <target_file> Copies the source file to the target. cp file1 file2 file3 ... dir Copies the files to the target directory (last argument). cp -i (interactive) Asks for user confirmation if the target file already exists cp -r <source_dir> <target_dir> (recursive) Copies the whole directory.
23
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
mv and rm commands
mv <old_name> <new_name> (move) Renames the given file or directory. mv -i (interactive) If the new file already exits, asks for user confirm rm file1 file2 file3 ... (remove) Removes the given files. rm -i (interactive) Always ask for user confirm. rm -r dir1 dir2 dir3 (recursive) Removes the given directories with all their contents.
24
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
25
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
26
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
27
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
28
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
29
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Symbolic links
A symbolic link is a special file which is just a reference to the name of another one (file or directory): Useful to reduce disk usage and complexity when 2 files have the same content. Example:
anakin_skywalker_biography -> darth_vador_biography
How to identify symbolic links: ls -l displays -> and the linked file name. GNU ls displays links with a different color.
32
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
33
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Hard links
The default behavior for ln is to create hard links A hard link to a file is a regular file with exactly the same physical contents While they still save space, hard links can't be distinguished from the original files. If you remove the original file, there is no impact on the hard link contents. The contents are removed when there are no more files (hard links) to them.
34
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
rm
File Inode
Hard link
rm
35
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Command documentation
36
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Command help
Some Unix commands and most GNU / Linux commands offer at least one help argument: -h (- is mostly used to introduce 1-character options) --help (-- is always used to introduce the corresponding long option name, which makes scripts easier to understand) You also often get a short summary of options when you input an invalid argument.
37
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Manual pages
man <keyword> Displays one or several manual pages for <keyword> man man Most available manual pages are about Unix commands, but some are also about C functions, headers or data structures, or even about system configuration files! man stdio.h man fstab (for /etc/fstab) Manual page files are looked for in the directories specified by the MANPATH environment variable.
38
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Info pages
In GNU, man pages are being replaced by info pages. Some manual pages even tell to refer to info pages instead. info <command>
info features:
Documentation structured in sections (nodes) and subsections (subnodes) Possibility to navigate in this structure: top, next, prev, up Info pages generated from the same texinfo source as the HTML documentation pages
39
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
40
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
42
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
43
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
44
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
45
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
47
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
48
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
File ownership
Particularly useful in (embedded) system development when you create files for another system. chown -R sco /home/linux/src (-R: recursive) Makes user sco the new owner of all the files in /home/linux/src. chgrp -R empire /home/askywalker Makes empire the new group of everything in /home/askywalker. chown -R borg:aliens usss_entreprise/ chown can be used to change the owner and group at the same time.
49
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
50
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
51
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
52
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Standard output
More about command output All the commands outputting text on your terminal do it by writing to their standard output. Standard output can be written (redirected) to a file using the > symbol Standard output can be appended to an existing file using the >> symbol
53
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
54
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Standard input
More about command input Lots of commands, when not given input arguments, can take their input from standard input. sort windows linux [Ctrl][D] linux windows sort takes its input from the standard input: in this case, what you type in the terminal (ended by [Ctrl][D])
sort < participants.txt The standard input of sort is taken from the given file.
55
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Pipes
Unix pipes are very useful to redirect the standard output of a command to the standard input of another one. Examples
cat *.log | grep -i error | sort grep -ri error . | grep -v ignored | sort -u \ > serious_errors.log cat /home/*/homework.txt | grep mark | more
56
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
57
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Standard error
Error messages are usually output (if the program is well written) to standard error instead of standard output. Standard error can be redirected through 2> or 2>> Example: cat f1 f2 nofile > newfile 2> errfile Note: 1 is the descriptor for standard output, so 1> is equivalent to >. Can redirect both standard output and standard error to the same file using &> : cat f1 f2 nofile &> wholefile
58
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
59
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
60
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
61
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
62
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Task control
63
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
64
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Processes
Everything in Unix is a file Everything in Unix that is not a file is a process Processes Instances of a running programs Several instances of the same program can run at the same time Data associated to processes: Open files, allocated memory, stack, process id, parent, priority, state...
65
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Starting a task: add & at the end of your line: find_prince_charming --cute --clever --rich &
66
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
fg fg %<n> Puts the last / nth background job in foreground mode Moving the current task in background mode: [Ctrl] Z bg kill %<n> Aborts the nth job.
67
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
68
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Process id Virtual process size (code + data + stack) Process resident size: number of KB currently in RAM Terminal Status: R (Runnable), S (Sleep), W (paging), Z (Zombie)...
69
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
You can change the sorting order by typing M: Memory usage, P: %CPU, T: Time. You can kill a task by typing k and the process id.
70
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
72
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
73
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Sequential commands
Can type the next command in your terminal even when the current one is not over. Can separate commands with the ; symbol: echo I love thee; sleep 10; echo not Conditionals: use || (or) or && (and): more God || echo Sorry, God doesn't exist Runs echo only if the first command fails ls ~sd6 && cat ~sd6/* > ~sydney/recipes.txt Only cats the directory contents if the ls command succeeds (means read access).
74
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Quoting (1)
Double (") quotes can be used to prevent the shell from interpreting spaces as argument separators, as well as to prevent file name pattern expansion. > echo "Hello World" Hello World > echo "You are logged as $USER" You are logged as bgates > echo *.log find_prince_charming.log cosmetic_buys.log > echo "*.log" *.log
75
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Quoting (2)
Single quotes bring a similar functionality, but what is between quotes is never substituted > echo 'You are logged as $USER' You are logged as $USER Back quotes (`) can be used to call a command within another > cd /lib/modules/`uname -r`; pwd /lib/modules/2.6.9-1.6_FC2 Back quotes can be used within double quotes > echo "You are using Linux `uname -r`" You are using Linux 2.6.9-1.6_FC2
76
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
77
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Environment variables
Shells let the user define variables. They can be reused in shell commands. Convention: lower case names You can also define environment variables: variables that are also visible within scripts or executables called from the shell. Convention: upper case names. env Lists all defined environment variables and their value.
78
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
79
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
80
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
82
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Alias
Shells let you define command aliases: shortcuts for commands you use very frequently. Examples
alias ls='ls -la' Useful to always run commands with default arguments. alias rm='rm -i' Useful to make rm always ask for confirmation. alias frd='find_rambaldi_device --asap --risky' Useful to replace very long and frequent commands. alias cia='. /home/sydney/env/cia.sh' Useful to set an environment in a quick way (. is a shell command to execute the content of a shell script).
83
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
84
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
~/.bashrc file
~/.bashrc
Shell script read each time a bash shell is started You can use this file to define Your default environment variables (PATH, EDITOR...). Your aliases. Your prompt (see the bash manual for details). A greeting message.
85
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Command editing
You can use the left and right arrow keys to move the cursor in the current command. You can use [Ctrl][a] to go to the beginning of the line, and [Ctrl][e] to go to the end. You can use the up and down arrows to select earlier commands. You can use [Ctrl][r] to search inside the history of previous commands.
86
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
87
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
88
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Text editors
89
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Text editors
Graphical text editors Fine for most needs nedit Emacs, Xemacs Kate, Gedit Text-only text editors Often needed for sysadmins and great for power users vi, vim nano
90
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
91
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
nedit screenshot
92
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Emacs / Xemacs
Emacs and Xemacs are pretty similar (up to your preference) Extremely powerful text editor features Great for power users Less ergonomic than nedit Non standard shortcuts Much more than a text editor (games, e-mail, shell, browser). Some power commands have to be learnt.
93
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
94
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
vi
Text-mode text editor available in all Unix systems. Created before computers with mice appeared. Difficult to learn for beginners used to graphical text editors. Very productive for power users. Often can't be replaced to edit files in system administration or in Embedded Systems, when you just have a text console.
95
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
vim - vi improved
vi implementation now found in most GNU / Linux host systems Implements lots of features available in modern editors: syntax highlighting, command history, help, unlimited undo and much much more. Cool feature example: can directly open compressed text files. Comes with a GTK graphical interface (gvim) Unfortunately, not free software (because of a small restriction in freedom to make changes)
96
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
vi basic commands
Though vi is extremely powerful, its main 30 commands are easy to learn and are sufficient for 99% of everyone's needs! You can also take the quick tutorial by running vimtutor. Get our vi memento sheet if you didn't get it with this course: http://free-electrons.com/docs/command-line
97
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
GNU nano
http://www.nano-editor.org/ Another small text-only, mouse free text editor. An enhanced Pico clone (non free editor in Pine) Friendly and easier to learn for beginners thanks to on screen command summaries. Available in binary packages for several platforms. An alternative to vi in embedded systems. However, not available as a busybox built-in.
98
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
99
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Compressing and archiving
100
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
du -h <file> (disk usage) -h: returns size on disk of the given file, in human readable format: K (kilobytes), M (megabytes) or G (gigabytes), . Without -h, du returns the raw number of disk blocks used by the file (hard to read). Note that the -h option only exists in GNU du. du -sh <dir> -s: returns the sum of disk usage of all the files in the given directory.
101
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
df -h Returns disk space information for all filesystems available in the system. When errors happen, useful to look for full filesystems.
102
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
103
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Archiving (1)
Useful to backup or release a set of files within 1 file tar: originally tape archive Creating an archive: tar cvf <archive> <files or directories> c: create v: verbose. Useful to follow archiving progress. f: file. Archive created in file (tape used otherwise). Example: tar cvf /backup/home.tar /home bzip2 /backup/home.tar
104
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Archiving (2)
Viewing the contents of an archive or integrity check: tar tvf <archive> t: test Extracting all the files from an archive: tar xvf <archive> Extracting just a few files from an archive: tar xvf <archive> <files or directories> Files or directories are given with paths relative to the archive root directory.
105
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
106
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
md5sum -c MD5SUM Checks the integrity of the files in MD5SUM by comparing their actual MD5 checksum with their original one.
107
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Printing
108
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Unix printing
Multi-user, multi-job, multi-client, multi-printer In Unix / Linux, printing commands don't really print. They send jobs to printing queues, possibly on the local machine, on network printing servers or on network printers. Printer independent system: Print servers only accept jobs in PostScript or text. Printer drivers on the server take care of the conversion to each printers own format. Robust system: Reboot a system, it will continue to print pending jobs.
109
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Printing commands
Useful environment variable: PRINTER Sets the default printer on the system. Example: export PRINTER=lp lpr [-P<queue>] <files> Sends the given files to the specified printing queue The files must be in text or PostScript format. Otherwise, you only print garbage. a2ps [-P<queue>] <files> Any to PostScript converts many formats to PostScript and send the output to the specified queue. Useful features: several pages / sheet, page numbering, info frame...
110
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
cancel <job#> [<queue>] Removes the given job number from the default queue.
111
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
112
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Synchronizing files
113
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
114
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
-a: archive mode. Equivalent to -rlptgoD... easy way to tell you want recursion and want to preserve almost everything.
rsync -Pav --delete /home/steve/ideas/ /home/bill/my_ideas/
-P: --partial (keep partially transferred files) and --progress (show progress during transfer) --delete: delete files in the target which don't exist in the source. Caution: directory names should end with / . Otherwise, you get a my_ideas/ideas/ directory at the destination.
115
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
116
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Comparing files and directories
117
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
tkdiff
http://tkdiff.sourceforge.net/ Useful tool to compare files and merge differences
119
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
kompare
Another nice tool to compare files and merge differences Part of the kdesdk package (Fedora Core)
120
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
gvimdiff
Another nice tool to view differences in files Available in most distributions with gvim Apparently not using diff. No issue with files with binary sections!
121
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Looking for files
122
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
123
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
124
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Miscellaneous
Various commands
125
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
126
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Changing users
You do not have to log out to log on another user account! su hyde (Rare) Change to the hyde account, but keeping the environment variable settings of the original user. su - jekyll (More frequent) Log on the jekyll account, with exactly the same settings as this new user. su When no argument is given, it means the root user.
127
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
128
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
wget examples
wget -c \ http://microsoft.com/customers/dogs/winxp4dogs.zip Continues an interrupted download. wget -m http://lwn.net/ Mirrors a site. wget -r -np http://www.xml.com/ldd/chapter/book/ Recursively downloads an on-line book for off-line access. -np: "no-parent". Only follows links in the current directory.
129
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
130
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
131
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Checksum commands
A checksum or hash sum is a fixed-size datum computed from an arbitrary block of digital data for the purpose of detecting accidental errors that may have been introduced during its transmissions or storage. http://en.wikipedia.org/wiki/Checksum The MD5 hash algorithm is implemented in the md5sum command
$ md5sum patch-2.6.24.7.bz2 0c1c5d6d8cd82e18d62406d2f34d1d38 patch-2.6.24.7.bz2
The SHA algorithm is implemented in the shaXsum (sha1sum, sha256sum, etc.) The integrity of several files can be verified against a file listing the checksums using the -c option.
132
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
System administration
See our presentation about system administration basics: Network setup Creating and mounting filesystems Accessing administrator (root) priviledges Package management Also available on http://free-electrons.com/docs/command-line
133
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Application development
134
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
For C++ : g++ -o test test.cc The -Wall option enables more warnings To compile sources files to object files and link the application : gcc -c test1.c gcc -c test2.c gcc -o test test1.o test2.o gcc automatically calls the linker ld
135
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Outside of the C library, thousands of other libraries are available for graphic programming, multimedia, networking, scientific computations, and moroe. Most libraries are already available as packages in your distribution, in general in two packages
libfoo is the package containing the library itself. This package is required to execute already compiled applications, but not sufficient to build new applications libfoo-dev is the package containing the headers and other configurations files and tools needed to build new applications relying on libfoo.
136
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
To compile your application with the library, the easiest solution is to use pkg-config, which is supported by most libraries today : gcc -o test test.c $(pkg-config --cflags libs) By default, the application are dynamically linked with the libraries
The libraries must be present in /lib/ for the application to work Use the ldd command to see which libraries are needed by an application
137
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
See http://www.gnu.org/software/make/manual/
138
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
139
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
The test target depends on t1.o and t2.o. Once these files are generated, clean: the gcc command is $(RM) -f test executed. $@ is the target name install: $^is the name of all $(CP) test /usr/bin dependencies. The .o files are generated using implicit These targets are executed by running make clean and make dependencies, known by make. install
140
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Build systems
Makefiles are nice, but they don't easily allow easy adaptation to the different build environment and different build options More elaborated build systems have been developed
Autotools (automake, autoconf), based on Makefiles and shell scripts. Even though they are old and a little bit difficult to understand, they are the most popular build system for free software packages. CMake, a newer, cleaner build system Sconcs and Waf, other build systems based on Python
The typical steps to compile a autotools based package are ./configure make sudo make install
141
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Debugging
The official debugger that comes with the GNU development tools is gdb.
See http://sourceware.org/gdb/download/onlinedocs/gdb.html
An application must be compiled with the -g option to be properly debugged. This option adds debugging information to the application binary gcc -o test test.c -g The application can then be run inside the gdb debugger : gdb test Or the debugger can be attached to the application while it is running : gdb test -p PID
Where PID is the process ID of the running application
142
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Using gdb
gdb is a text-based debugger, with a command-line interface like a shell, providing dedicated commands. Some of the important commands are :
break (b) to set a breakpoint in the code. Can be used with a function name or a location in the source code, or an absolute memory address. print (p) to print the value of a variable. Used with a variable name, even if it's a complex one (which involves dereferencing structures, for example) c to continue the execution until the next breakpoint. next (n) to execute only the next line of code (step over any function call) and step (s) to execute only the next line of code (step into any function call) backtrace (bt) to display the function call stack
143
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
http://www.gnu.org/software/ddd/
145
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
146
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
147
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Generated files should never be versioned. Version control is absolutely necessary when working as a team on a project
Version control systems are one of the fundamental tools used for open source development
149
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Centralized approach
The centralized approach is based on the presence of a server hosting a repository. The repository contains the history of all files and acts as the reference for all participants in the project. Typical client/server approach.
A'' A' A
Repository
A' A Linus
A'' A Richard
151
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
In the free/open source projects, the traditional model is the merge one. It avoids the burden of handling the locking and scales better with a bigger team. But CVS and Subversion both implement advisory locking : a developer must voluntarily lock the files he is going to work with.
152
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
A* A'' A
A* A' Linus
A''
A* A'' A Richard
153
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
CVS
Free Software Used to be the most widely used free version control system. Now replaced by Subversion. Still used by big projects, but a lot of large projects (KDE, Apache, Eclipse) have moved to Subversion. Homepage : http://www.nongnu.org/cvs/ Documentation : http://ximbiot.com/cvs/manual/
154
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
CVS usage
The main client is a command-line one, implemented in the cvs command
cvs help gives the list of available commands
add, admin, annotate, checkout, commit, diff, edit, editors, export, history, import, init, log, login, logout, ls, pserver, rannotate, rdiff, release, remove, rlog, rls, rtag, server, status, tag, unedit, update, version, watch, watchers
Graphical interfaces
Integration in Vim, Emacs, Anjuta, Dev-C++, Eclipse, Kdevelop, etc. Graphical clients: Cervisia (for KDE), gcvs (for Gnome), CrossVC, etc. Web clients: ViewVC, cvsweb, etc.
155
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
156
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Simple usage
Working copy creation to be done once for all ! Modifications of the project files
Files are modified directly, as usual, nothing special is necessary. CVS knows the contents of the original version of the files and is able to compute the list of modifications made by the developer. Every directory contains a CVS directory that should not be modified.
Example
$ cvs -d :local:/tmp/repo/ checkout project cvs checkout: Updating project U project/README U project/a.c U project/b.c $ cd project/ project$ vi README project$ cvs commit -m "Adding infos" cvs commit: Examining . /tmp/repo/project/README,v <-- README new revision: 1.3; previous revision: 1.2 project$ cvs update cvs update: Updating . U a.c project$
158
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
File management
When new files or directories are created inside the project, they are not automatically taken into account by CVS They must be explicitly added with the add command
cvs add file1.c The new file is not propagated to the repository until the next commit Same thing with directories
Files and directories can be removed with the remove command Issues fixed in Subversion
Files and directories cannot be renamed without losing the history Directory removal is badly handled in CVS
159
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Example
project$ vi c.c project$ cvs add c.c cvs add: scheduling file `c.c' for addition cvs add: use `cvs commit' to add this file permanently project$ cvs commit -m "New file c.c" cvs commit: Examining . /tmp/repo/project/c.c,v <-- c.c initial revision: 1.1 project$ rm a.c project$ cvs remove a.c cvs remove: scheduling `a.c' for removal cvs remove: use `cvs commit' to remove this file permanently project$ cvs commit -m "Remove a.c" cvs commit: Examining . /tmp/repo/project/a.c,v <-- a.c new revision: delete; previous revision: 1.2
160
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
To see the modifications made to the project and not committed yet, use the diff command Repository checkout Working copy diff
161
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
update
162
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
A graphical interface, either a graphical client or a web client, will be very useful to navigate and use the history efficiently
163
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
The file has been modified on the server since our last update. So we must make merge the modifications we have done with the modifications of the other developers, by updating our working copy.
$ cvs update cvs update: Updating . RCS file: /tmp/repo/project/main.c,v retrieving revision 1.1 retrieving revision 1.2 Merging differences between 1.1 and 1.2 into main.c rcsmerge: warning: conflicts during merge cvs update: conflicts found in main.c C main.c
164
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
165
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
166
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Repository initialization
A repository must be initialized using the init command :
mkdir /home/user/cvsrepo cvs -d :local:/home/user/cvsrepo init
Once initialized, it can be accessed locally or remotely through SSH. Remote access through pserver will require additional configuration, see the CVS documentation. If already existing projects have to be imported in the repository, use the import command
cd project cvs -d :local:/home/user/cvsrepo import modulename vendortag releasetag
vendortag, symbolic name for the branch releasetag, symbolic name for the release
167
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Tags are created using the cvs tag command. Branches allow to create parallel flow of developments
Maintenance of a previous release Development of experimental new features
Created using the -b option of the cvs tag command Branching and merging is relatively complicated, and falls outside the scope of this training.
168
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
IRC
Send a message at every commit. Notifies of the changes.
Version control
At every commit, a build and test machin builds and tests the project.
Link the commits with the bugs and the bugs with the commits.
Bug tracking
169
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Why Subversion ?
Because CVS has many drawbacks and short-coming
Not possible to simply rename files and directories while preserving the history No atomic commits identifying the commit as a whole, making it difficult to navigate in the history, revert commits, and is the source of repository incoherency in case of crashes Poor branching and merging capabilities
In usage, Subversion is very similar to CVS : the commands are exactly the same.
Fixes all CVS short comings And provides interesting branching and merging features such as merge-tracking. Many large-scale projects already made the switch.
170
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
More and more commonly used in free software projects (Linux kernel, X.org, etc.) Most commonly used tools : Git, Mercurial
171
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Related documents
All our technical presentations on http://free-electrons.com/docs Linux kernel Device drivers Architecture specifics Embedded Linux system development
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
How to help
You can help us to improve and maintain this document... By sending corrections, suggestions, contributions and translations By asking your organization to order development, consulting and training services performed by the authors of these documents (see http://free-electrons.com/). By sharing this document with your friends, colleagues and with the local Free Software community. By adding links on your website to our on-line materials, to increase their visibility in search engine results.
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http//free-electrons.com
Linux kernel Linux device drivers Board support code Mainstreaming kernel code Kernel debugging Embedded Linux Training All materials released with a free license! Unix and GNU/Linux basics Linux kernel and drivers development Real-time Linux, uClinux Development and profiling tools Lightweight tools for embedded systems Root filesystem creation Audio and multimedia System optimization
Free Electrons
Our services
Custom Development System integration Embedded Linux demos and prototypes System optimization Application and interface development Consulting and technical support Help in decision making System architecture System design and performance review Development tool and application support Investigating issues and fixing tool bugs