Thursday, 16 October 2025
Extended Arguments (xargs) Unix command
Sunday, 19 May 2024
Linux file-system permissions
File & Directory Ownership
To see permissions (r= read, w=write, x=execute) and ownership (user:group) over some file or directory use:$ ls -la
Example:
$ sudo chown -R $USER directory
or
$ sudo chown -R username:group directory
-R stands for --recursive.
To change user:group of a file:
$ sudo chown user:group file_name
Permissions
chmod with symbolic modes
u = userg = groupo = otherplus (+) symbol adds a permissionminus (-) symbol removes a permissionr = readw = writex = execute
chmod with modes as an octal number
read = 4write = 2execute = 1no permissions = 0
- 600 – owner can 6 = read(4) + write(2), group and others have no permissions on the file
- 700 – owner can 7 = read(4) + write(2) + execute(1), group and others have no permissions on the file
- 444 = owner, group and all can read(4) only
- 666 – owner can 6 = read(4) + write(2), group can 6 = read(4) + write(2), all/anyone can 6 = read(4) + write(2)
- 755 – owner can 7 = read(4) + write(2) + execute(1), group can 5 = read(4) + execute(1), all/anyone/world can 5 = read(4) + execute(1)
- 777 – owner can = read(4) + write(2) + execute(1), group can 7 = read(4) + write(2) + execute(1), all/anyone can 7 = read(4) + write(2) + execute(1)
$ chmod 600 filename
$ chmod +x script.sh
Typical permissions in Linux file-system
Which ownership allows object creation or deletion?
Whether a file can be deleted or not is not a property of the file but of the directory that the file is located in. A user may not delete a file that is located in a directory that they can't write to.Files (and subdirectories) are entries in the directory node. To delete a file, one unlinks it from the directory node and therefore one has to have write permissions to the directory to delete a file in it.The write permissions on a file determines whether one is allowed to change the contents of the file.The write permissions on a directory determines whether one is allowed to change the contents of the directory.
References:
Introduction to GNU Wget tool
Wget is a tool that retrieves content from web servers.
$ wget --output-document=downloaded_file.txt www.example.com/file.txt
$ wget -output-document=google_index.html www.google.com
--2024-05-19 00:00:49-- http://www.google.com/
Resolving www.google.com (www.google.com)... 172.217.169.4, 2a00:1450:4009:817::2004
Connecting to www.google.com (www.google.com)|172.217.169.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘STDOUT’
- [<=> ] 0 --.-KB/s <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"
...
</script></- [ <=> ] 19.93K --.-KB/s in 0.02s
2024-05-19 00:00:50 (818 KB/s) - written to stdout [20411]
-O file = outputs the downloaded content into the file or device (e.g. /dev/null)
-o file = outputs log information to file
Resources:
Sunday, 5 May 2024
Working with files in Linux
Creating a file
To create a file use touch:
$ touch filename
Writing into a file
- > will overwrite existing file or crate a new file
- >> will append text to existing file or create a new file
> file.txt
What does “>” do vs “>>”?
tee command
Writing multi-line text into file
shell - How to append multiple lines to a file - Unix & Linux Stack Exchange
Ending file with new line character
[No newline at end of file]
It is a good style to always put the newline as a last character if it is allowed by the file format.
Unix historically had a convention of all human-readable text files ending in a newline. Reasons:
Practically, because many Unix tools require or expect it for proper display.
Philosophically, because each line in a text file terminates with an "end-of-line" character--the last line shouldn't be any exception.
To write into file a set of lines which end with a new line character:
$ echo $'first line\nsecond line\nthirdline\n' > foo.txt
$'...' construct expands embedded ANSI escape sequences
How to put a newline special character into a file using the echo command and redirection operator?
Getting the information about a file
To get the number of lines (well, newline characters) in the file:
$ wc -l myfile.txt
23 myfile.txt
(This is why it's important to follow the convention and end each line with newline character.)
To get the number of words on some webpage:
$ curl "https://example.com/" 2>/dev/null | grep -i "word1|word2" | wc -l
To see the last couple of lines in the file use command tail:
$ tail myfile
To find various hash sums of a file:
$ md5sum file_name
$ sha1sum file_name
$ sha256sum file_name
If file is Windows executable, it is possible to examine it with:
Checking whether file exists
if test -f "$symlink_file"; then
echo "$symlink_file" exists and is regular file.
else
echo "$symlink_file" does not exist or is not a regular file.
fi
if test -L "$regular_file"; then
echo "$regular_file" exists and is symlink file.
else
echo "$regular_file" does not exist or is not a symlink file.
fi
How to Check if a File or Directory Exists in Bash
Copying files
cp - copy
cp [OPTIONS] SOURCE DEST
SOURCE - file or directory
DEST - file or directory
An error is reported if directory is specified as source and file as destination.
$ cp -r test test.txt
cp: cannot overwrite non-directory 'test.txt' with directory 'test'
Copying files and directories to/from remote machine
Moving files
$ mv *.{jpg,gif,png} ~/Pictures
Renaming files
To rename all .new files in the current directory to *.old:
$ rename -f -v 's/.new/.old/' *
-f = force; allows overwriting existing *.old files
-v = verbose
File viewing and editing
To simply view the content of some file, use cat:$ cat filename
To edit some file, you can use vi editor. Example:
$ vi ~/.profile
sudo gedit ~/.profile
To see the content of the file as binary and hexadecimal:
xxd -b file
xxd file
Searching for Files
To search file from the root directory use /:
$ find / -name "file_name.ext"
To find any file or directory which contains some string in their name, recursively, starting from the current directory:
Searching for text across files
How do I find all files containing specific text on Linux?
man grep
$ grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R = recursive,
-n = line number
-w = match the whole word.
-l (lower-case L) = just give the file name of matching files
$ find ./go/src/pkg -type f -name "*.go" | xargs egrep '^type.*(er|or) interface {'
Extended Arguments (xargs) Unix command | My Public Notepad
egrep manual - egrep prints lines matching a pattern
Comparing Files
How to ignore line endings when comparing files?
$ diff --strip-trailing-cr file1 file2
How to detect file ends in newline?
Working with executable files
Running a command prefixed by the time command will tell us how long our code took to execute.
$ time myapp
real 0m13.761s
user 0m0.262s
sys 0m0.039s
If an executable is present but some of its dependencies are missing bash (or sh) might display an error messages stating that main executable is not found (which might be a bit misleading).
Example:
/ # ls
bin myapp data-vol dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # myapp
/bin/sh: myapp: not found
Wednesday, 24 April 2024
Installing Software on Linux
How to install software available in Package Repository?
Installing from Ubuntu Package Repository
Example: Installing VLC player$ sudo apt-get update
$ sudo apt-get install vlc
It's best to run sudo apt-get update first as this updates local information about what packages are available from where in what versions. This can prevent a variety of installation errors (including some "unmet dependencies" errors), and also ensures you get the latest version provided by your enabled software sources.
There is also an apt version of this command:
$ sudo apt update
...
Reading package lists... Done
Building dependency tree
Reading state information... Done
23 packages can be upgraded. Run 'apt list --upgradable' to see them.
Installing from non-default (3rd Party) Package Repository
Example: Installing PowerShell from Microsoft Package Repository
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh
If you install the wrong version of packages-microsoft-prod.deb you can uninstall it with:
$ sudo dpkg -r packages-microsoft-prod
(Reading database ... 254902 files and directories currently installed.)
Removing packages-microsoft-prod (1.0-3) ...
How to install software distributed via Debian package (.deb) files?
$ sudo dpkg -i /path/to/deb/file
$ sudo apt-get install -f
The latter is necessary in order to fix broken packages (install eventual missing/unmet dependencies).
How to install a deb file, by dpkg -i or by apt?
Debian and Ubuntu based Package Repository (GNU/Linux x86/x64)
Add Etcher debian repository:
echo "deb https://deb.etcher.io stable etcher" | sudo tee /etc/apt/sources.list.d/balena-etcher.list
Trust Bintray.com's GPG key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61
Update and install:
sudo apt-get update
sudo apt-get install balena-etcher-electron
Uninstall
sudo apt-get remove balena-etcher-electron
sudo rm /etc/apt/sources.list.d/balena-etcher.list
sudo apt-get update
How to install applications distributed via snaps?
Snaps are containerised software packages. They're designed to install the programs within them on all major Linux systems without modification. Snaps do this by developers bundling a program's latest libraries in the containerized app.Snap updates automatically and carries all its library dependencies, so is a better choice for users who want ease of deployment and to stay on top of the latest developments.
Snapcraft - Snaps are universal Linux packages
Installing snap on Ubuntu | Snapcraft documentation
How to Install and Use Snap on Ubuntu 18.04 - codeburst
0755 permissions mean the following: user can 7=read(4)+write(2)+execute(1); group can 5=read(4)+execute(1); anyone/world can 5=read(4)+execute(1)
Thursday, 18 April 2024
Cron Utility (Unix)
How to disable some cron job?
How to disable all cron jobs?
Resources:
cron - WikipediaThursday, 11 April 2024
Linux Interview Questions
- How to check if any application is listening on port 80?
- How to list all the environment variables?
- How to list all the variables, including all local variables?
- How to reference a variable?
- How to print the value of a particular variable?
- How to set an environment variable? How to export it to the global environment (available to other processes). How should variable be enclosed if it contains spaces?
- How to set a local variable?
- Where are local variables available?
- How to unset a local variable?
- In which directory to place application's source code? (What shall be a destination for COPY of the source code when building a Linux-based Docker image?)
- How to edit a file if vi, vim, gedit etc...are not available?
- How to list all users on the system?
- To be continued...
Tuesday, 20 December 2022
Installing Node via Node Version Manager on Ubuntu
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15916 100 15916 0 0 23135 0 --:--:-- --:--:-- --:--:-- 23133
=> Downloading nvm from git to '/home/bojan.komazec/.nvm'
=> Cloning into '/home/bojan.komazec/.nvm'...
remote: Enumerating objects: 356, done.
remote: Counting objects: 100% (356/356), done.
remote: Compressing objects: 100% (303/303), done.
remote: Total 356 (delta 39), reused 164 (delta 27), pack-reused 0
Receiving objects: 100% (356/356), 222.15 KiB | 417.00 KiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/bojan.komazec/.bashrc
=> Appending bash_completion source string to /home/bojan.komazec/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
After this, re-open the terminal and type:
$ nvm
Node Version Manager (v0.39.2)
Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
Any options that produce colorized output should respect the `--no-colors` option.
Usage:
nvm --help Show this message
--no-colors Suppress colored output
nvm --version Print out the installed version of nvm
nvm install [<version>] Download and install a <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm install`:
-s Skip binary download, install from source only.
-b Skip source download, install from binary only.
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
--skip-default-packages When installing, skip the default-packages file if it exists
--latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
--no-progress Disable the progress bar on any downloads
--alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)
--default After installing, set default alias to the version specified. (same as: nvm alias default <version>)
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [<version>] Modify PATH to use <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm use`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm exec`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [<version>] [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm run`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls [<version>] List installed versions, matching a given <version> if provided
--no-colors Suppress colored output
--no-alias Suppress `nvm alias` output
nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
--no-colors Suppress colored output
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
--silent Silences stdout/stderr output
nvm alias [<pattern>] Show all aliases beginning with <pattern>
--no-colors Suppress colored output
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available and version is omitted.
--silent Silences stdout/stderr output when a version is omitted
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
nvm set-colors [<color codes>] Set five text colors using format "yMeBg". Available when supported.
Initial colors are:
b y g r e
Color codes:
r/R = red / bold red
g/G = green / bold green
b/B = blue / bold blue
c/C = cyan / bold cyan
m/M = magenta / bold magenta
y/Y = yellow / bold yellow
k/K = black / bold black
e/W = light grey / white
Example:
nvm install 8.0.0 Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3
nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3
nvm alias default 8.1.0 Set default node version on a shell
nvm alias default node Always default to the latest available node version on a shell
nvm install node Install the latest available version
nvm use node Use the latest version
nvm install --lts Install the latest LTS version
nvm use --lts Use the latest LTS version
nvm set-colors cgYmW Set text colors to cyan, green, bold yellow, magenta, and white
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
Let's now check what's the latest version of Node.js:
$ nvm install 19.3.0
Downloading and installing node v19.3.0...
Downloading https://nodejs.org/dist/v19.3.0/node-v19.3.0-linux-x64.tar.xz...
############################################################################################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v19.3.0 (npm v9.2.0)
Creating default alias: default -> 19.3.0 (-> v19.3.0)
Let's verify the installation:
$ node -v
v19.3.0
---






