Filenames
Hidden Files
Q. How do you “hide” a file or directory?
Name the file with a .
at the beginning:
$ touch .hidden
Q: How do you list such files/directories?
Use the -a
option:
$ ls -a
- Contributor(s)
- Travis
Dash Files
Q: How can you remove a file named -r
?
Use --
to tell commands where options end:
$ touch -- -r
$ rm -- -r
$ rm ./-r
- Contributor(s)
- Bruno
- Mattia
- Quiz(es)
- Meetings:2014:05
Glob Files
Q: How can you remove a file named *
or ?
Use backslashes to escape such characters:
$ touch \* \?
$ rm \* \?
$ rm '*' '?'
- Contributor(s)
- Daniel
- Quiz(es)
- Meetings:2014:05
Globs
Q: How can .??*
be useful?
It displays hidden files with at least two characters after the dot:
$ touch .1 .un .deux .trois
$ echo .*
. .. .1 .deux .trois .un
$ echo .?*
.. .1 .deux .trois .un
$ echo .??*
.deux .trois .un
- Contributor(s)
- Bruno
- Quiz(es)
- Meetings:2014:05
Permissions
-rwxrwxrwx
Q: How can you find all world-writable files/directories in $HOME
?
find
is used for recursion:
$ find $HOME -perm -2 ! -type l -ls
Q: How can you fix the permissions for all files found?
Use -exec chmod
:
$ find $HOME -perm -2 ! -type l -exec chmod o-w {} +
$ find $HOME -perm -2 ! -type l -exec chmod o-w {} \;
What is the difference between “+
” and “\;
” notations? Why would you choose one over the other?
- Contributor(s)
- Travis
- Bruno
- Quiz(es)
- Meetings:2014:05
-rwsr-x---
Q: What do permissions -rwsr-x---
signify?
- SETUID
- When users in the group execute the file, it executes as the file owner.
chmod 4750 file
Q: When would permissions like this be useful?
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
-rwxr-sr-x
Q: What do permissions -rwxr-sr-x
signify?
- SETGID
- When users execute the file, it executes as the file group.
chmod 2755 file
Q: When would permissions like this be useful?
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
drwxrwsr-x
Q: What do permissions drwxrwsr-x
signify?
- SETGID
- When files/directories are created in this directory, they are assigned the same group, not the user's default group.
chmod 2775 directory
Q: When would permissions like this be useful?
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
drwxrwxrwt
Q: What do permissions drwxrwxrwt
signify?
- sticky bit
- When files/directories are created in this directory, they can only be unlinked by the owner or root.
chmod 1777 directory
Q: When would permissions like this be useful?
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
/etc/hostname
Q: How can you update /etc/hostname
as a non-root user?
$ ls -l /etc/hostname
-rw-r--r-- 1 root root ... /etc/hostname
$ cat /etc/hostname
changeme
Use sudo tee
:
$ echo tlug | sudo tee /etc/hostname
Alternatively:
$ sudo bash -c 'echo tlug > /etc/hostname'
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
Filesystem
rm
Q: How and when could rm
not free disk space?
- Files are reference-counted.
- Disk space is not freed by
rm
if the file is in use by a process. - The space should be freed when the process finishes.
- Contributor(s)
- Bruno
- Quiz(es)
- Meetings:2014:05
Links
Q: What is the difference between ln
and ln -s
?
- By default,
ln
creates hard links:- Links to inodes on the same filesystem
- With
-s
,ln
creates symbolic links:- Links can hold arbitrary text
- Links are resolved when referenced
Q: When would you use each type of link?
- Contributor(s)
- Bruno
- Quiz(es)
- Meetings:2014:05
Pipes
Standard IO
Q: How can you run a program and not display STDERR
(bash)?
Redirect STDERR
to /dev/null
:
$ somejavaprogram 2> /dev/null
Q: How can you redirect both STDOUT
and STDERR
to a file?
$ somejavaprogram > file.log 2>&1
Q: What are doing exactly the following lines?
TMPFILE=/tmp/$$-test
exec 3>&1 >${TMPFILE}
echo ichi
exec 1>&3 3>&-
echo ni
cat ${TMPFILE}
- Contributor(s)
- Travis
- Bruno
- Quiz(es)
- Meetings:2014:05
Named Pipes
Q: How do you create a named pipe?
$ mkfifo tlug
$ ls -l tlug
prw-r--r-- 1 tokyo tokyo 0 May 10 15:00 tlug
Q: How do you use named pipes?
Treat them like files. On the command line, use redirection:
one$ echo 'Hello, TLUG!' > tlug
two$ cat < tlug
Nothing is written to disk!
Q: Why use named pipes?
- Fast inter-process communication (IPC)
- Use more than one pipe without using temporary files
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
Processes
Slow System
Q: How can you determine which program is slowing down your system?
- Use
top
to locate processes with high CPU/memory usage. - Use
iotop
to locate processes with high disk usage.
Q: How can you terminate it?
$ kill $PID # sends TERM signal to $PID
Q: What if that doesn't work?
$ kill -9 $PID # sends KILL signal to $PID
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
Unmount Fail
Q: If you try to unmount a filesystem and get an error about open files, how can you solve the problem?
Use lsof
to show which processes access a directory/file:
$ lsof /mnt/stick
Use fuser
to show which processes use the filesystem:
$ fuser -v -m /mnt/stick
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
Processes/Zombies
Q: What is a process? How do you create one? What is the relationship between a child process and a parent process?
Q: Who is the ancestor of all processes on your system? How could it be “alive” itself?
Q: What is a zombie process?
- When a process exits, its process descriptor stays in memory until its parent process calls
wait()
, which deletes it. A process in this state is a zombie process.
Q: Why are they bad?
- A process descriptor takes a negligible amount of memory.
- The zombie takes up a process ID (PID).
Q: How do you get rid of one?
Send a SIGCHLD
signal to the parent process:
$ kill -s SIGCHLD $PID
If that does not work, the parent process must be killed.
- Contributor(s)
- Travis
- Bruno
Substitution
Q: What does the following do (bash)?
$ program --in1 <(makein in1.txt) \
--in2 <(makein in2.txt) \
--out1 out1.txt --out2 out2.txt \
> stdout.txt 2> stderr.txt
<()
runs a process and substitutes a dynamically-assigned file descriptor.- The
<()
commands are run in parallel, managed by the OS.
- Contributor(s)
- Travis
- Quiz(es)
- Meetings:2014:05
Network
Infiltrated
Q: How can you determine which program is making your fiber-modem blink?
lsof
is useful for listing network connections as well:
$ lsof -i
netstat
is also a useful utility:
$ netstat -pt
- Contributor(s)
- Travis
- Reference(s)
- http://www.danielmiessler.com/study/lsof/
Ding
Q: If your terminal bell works, what standard utilities can you use to get an alert when somebody visits your website?
tail -f
follows files, and sed
can insert a BEL
:
$ tail -f /var/log/nginx/site.access.log \
| sed 's/^/\a/'
- Contributor(s)
- Travis