Week 3 Flashcards
executing multiple commands?
1)command1;command2;command3;
each command will be executed one after other even if any one or more commands have error or failed to execute
2) command1 && command2
Command 2 will be executed only if command1 succeeds
3) command1 || command 2
command2 will not be executed if command1 succeeds
how to execute multiple commands in a subshell and then return their result to the original shell?
using parenthesis, we could do this.
e.g.: (ls, date, we filename.txt)
What is the name of the variable which gives the shell level in which the command is executed? Why do we need subshell?
$BASH_SUBSHELL , We need subshells so that we could confine the running environment of certain commands.
What are file descriptors?
There are 3 file descriptors.
Stdin (0) - Input stream normally keyboard
Stdout(1) - Output stream- usually monitor
stderr (2) - usually points to the monitor.
In linux, we assume that all these file descriptors are streaming characters which could be redirected to a file or a command.
What is the redirection operator?
command > file 1 (Stdout is redirected to a file). New file 1 will be created if it does not exist already.. if it already exists then it will be overwritten (Caution) , Note: write permission is required for the current directory else it wont work
Which command is used for displaying the hardware infor of the system?
hwinfo
what is the output for cat > file1
cat enables stdin and what ever we enter via keyboard will go into the file1. we could come to pwd by pressing Ctrl +D (End of File)
what happens when we execute just the cat command?
cat takes input from keyboard and displays it back to screen. We could come out by pressing Ctrl+D
How to append to a file?
using double redirection “»” . If file does not exist.. it will be created and if it exists, then the new content will be appended and NOT replaced, if write permissions are given.
e.g: date»_space; file2; wc-l /etc/profile»_space; file2; file /usr/bin/znew»_space; file2;
How to redirect error stream to file? can we send output to a file and error to a different file?
ls $HOME / blah 2>error.txt
ls -$HOME / blah > output.txt 2>error.txt
ls -R /etc (Recursively traverses into the directory etc)
How to serve as a file as std input (ie., serve file as input stream when the program is expecting input from keyboard)
cat < error.txt (will serve error.txt as input to command cat)
How to redirect output and error to the same file?
command > file1 2>&1
What happens when we use pipes with commands?
command1 | command 2
Stdout of command1 will become stdin of command2
ls /usr/bin | wc-l
combines commands.
one command line with multiple commands and pipes are possible.
How to use redirection, pipes together?
command1 | command2 > file1
stdout of command1 fed into command 2 as stdin and stdout of command 2 is fed as stdin of file1
How is /dev/null special?
It is a sink for output to be discarded. Use! silent and clean scripts.
command > file1 2> /dev/null
this command output will be written to file 1 and any error will be written to null file and subsequently be discarded.
what does tee command do?
tee command reads from stdin and writes to stdout and files.
could be combined along with pipe
Tee command could be used to write multiple files.
e.g : ls $HOME | tee file1 file2
ls $HOME //blah 2> /dev/null | tee file1 file2 | wc-l
What are package management systems? why do we need one?
- Tools for installing, updating, removing, managing software
1a. Install new/updated software across network. - Package - File lookup, both ways.
- Database of packages on the system including versions
- Dependency checking
- Signature verification tools.
- Tools for building packages.
What are major package types?
RPM (RedHat - CentOS, Fedora, Oracle Linux, openSUSE), SUSE Enterprise Linux
* for Maintaining Linux servers.. this will be helpful
DEB (Debian - Knoppix, Ubuntu - Mint
* for manintaining regular ubuntu installations , this will be helpful.
How to find the current operating system running on our machine?
lsb_release -a
What are some of the software architectures for which linux is available?
- amd 61 | x86_64
- i386 |x86
- ARM
- ppc64el | Open POWER
- all |noarch|src
What are some of the package tools?
- RPM (YUM- Yellowdog Updater Modifier) - RedHat Package manager(RPM), Dandified YUM (dnf)
- DEB - Synaptic(graphical), aptitute(Command line) - Advanced Package Tool (apt) - dpkg, kpka-deb
How to inquire package database?
search packages for a keyword
apt - cache search keyword
How to list all packages currently installed in our system?
apt -cache pkgnames
apt-cache pkgnames nm
How to display package recrods of a package?
apt -cache show -a package
apt-cache show nmap
What are the notations for package name of RPM and DEB package types?
RPM - Package-version-release.architecture.rpm
DEB - package _version-revision_architecture.deb
What are package priorities?
- required - essential to proper functioning of the system.
- important - provides functionality that enables the system to run well
- standard - included in a standard system installation
- optional - can omit if you do not have enough storage
- extra - could conflict with packages with higher priority, has specialized requirements, install only if needed.
What are package sections?
these are pre-bundled category of packages e.g: web, admin ulities, Games, GNOME, GNU, Phython, Ruby, Video etc.
What are some of the checksums ?
- md5sum (128 bit)
- SHA1 (160 bit)
- SHA256(256 bit)
In Ubuntu, what is the administrators group referred as?
sudo
Only sudoers can install /upgrade/remove packages
Where is all sudoers listed?
/etc/sudoers
how a superuser can find out unsuccessful sudo attempts
through log file /var/log/auth.log
where is authentic sources list file from where packages will be downloaded will be stored?
/etc/apt
Files: sources.list
Folder: sources.list.d (Contains sources from other programs like google, Teamviewer etc..) - as and when updates are released, package manager will update.
How to downloaded the updates and keep it in cache?
sudo apt-get update
The updates applicable only to your installed packages are used .
How to install the updates downloaded to cache?
sudo apt-get upgrade
After updating the existing packages. How to remove the unused but downloaded package updates in cache?
sudo apt-get autoremove
-removes outdated versions and unwanted files.
apt-get clean
clean local repository of retrieved package files
apt-get purge <package>
purge package files from system</package>
How to uninstall a package?
sudo apt-get remove <pkgname></pkgname>
How to install a package?
sudo apt-get install <package_name></package_name>
sudo apt-get reinstall <package-name></package-name>
where could we find details about the dpkg package manager?
/var/lib/dpkg
Files: arch, available, status
Folder: info
How to list all dpkg packages?
dpkg -l pattern
How to list installed files that came from packages?
dpkg -L package
How do find dpkg status of packages?
kpkg -s package
How to search installed packages for a file?
dpkg -S pattern
How to query dpkg database?
dpkg-query -W -f=’${section} {binary:Package}\n’
-w : show
-f ; format
lists the package section and naame of the executable/package.
How to filter an output?
grep <filter></filter>
What is “Sleep” command do?
Delays/sleep for specific seconds
how to run processes in background?
coproc sleep 10
we will get prompt immediately on enter. we could run other commands and sleep command will run in background.
which command lists all the processes currently running ?
ps
ps -e (all process running in computer.. and not just this shell)
jobs
ps –forest (tree view)
coproc is a shell keyword (found by using command ‘type’) but there is no man for coproc. Why?
because for shell commands we need to use help command.
help coproc
how to get the process id of the current shell?
echo $$
how to kill a process?
kill -9 <process_id></process_id>
kill -9 <process_id> & (backgound kill)</process_id>
If process is running in foreground then we could use ctrl +c to kill the process.
How to see all processes of OS?
top (Screen gets refreshed based on cpu activity)
q or Ctrl+c to exit
Ctrl +z for suspend to cmd line
How to suspend a process?
Ctrl + z
How to move a process from the background to fore-ground
command ‘fg’
What does the shell variable $- contain?
says under what selected options the current bash is running.
bash -c “echo $-“ - displays different text as the options are modified.
How to see the list of all historical commands run on the computer?
use command “history” . It will list shortcut numbers so that it could be used to repeat the operation/command. we could run the historical command by using !<shortcut/line no>
What is expansion command?
echo {a..z} lists all characters from a to z
echo {A..D}{A..D} will give all combinations from AA till DD
echo * (expansion of all directories in pwd)
echo d* (expansion of all directories in pwd staring with d) - case sensitive
How is “;” used to run multiple commands in left to right order?
ls ; date; wc -l /etc/profile
”;” - aka command separator
how to find the status of the previously run command?
echo $?
Range : 1 -255
0 sucessful
1-255 - error
1 - permission denied
127 - command not found
137 - process killed using kill command
we could manually send a exit code from a shell inside a shell. if number is bigger than 256 then number %256 will be displayed.
how to open a calculator in ubuntu?
bc
Ctrl + d to come out of calc
How to declare a list of items and retrieve from list?
declare -A caparray=( [Switzerland]=Bern [Germany]=Berlin [Canada]=Ottawa [Tokyo]=Japan [Mongolia]=Ulaanbaater)
echo ${!caparray[@]}
echo ${caparray[Tokyo]:1:2}
There are three files master.txt, half1.txt and half2.txt in the current working directory. Add first 2 lines of half1.txt to the file master.txt at the end(starting at a new line) then append the last 3 lines of the file half2.txt to the file master.txt at the end(starting at a new line). Append the lines in the sequence mentioned.
head -2 half1.txt»master.txt
tail -3 half2.txt»master.txt