Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Thursday, 16 October 2025

Extended Arguments (xargs) Unix command




xargs builds and executes command lines from standard input.

While pipe command (|) takes the stdout of the previous command and forwards it to stdin of the next command, xargs takes space-separated strings from that stdin and convert them into arguments of xargs command.

Example:

$ echo "/dirA /dirB" | xargs ls

will be converted to:

ls /dirA /dirB

-n1 causes the command specified by xargs to be executed, taking one argument at a time from its input (arguments can be each in one line or simply separated by spaces or tabs) and running the command separately for each individual item.​ The -n1 option means "use just one argument per command invocation".​ Each item from standard input (for example, a filename from a list) will be passed separately to the command. As a result, the command will be run as many times as there are items in the input, once for each.

Example:

echo -e "a\nb\nc" | xargs -n1 echo

...will produce:

echo a
echo b
echo c

So each invocation receives only one argument from the input.​

This is useful when a command should process only one item at a time, such as deleting files one by one, or when handling commands that cannot accept multiple arguments simultaneously.

-I{} replaces {} in the command with the input item (in the following example that's lambda function name).

In the following example we use -I to replace {} with incoming argument for xarg and then we use $ positional parameters to interpolate inputs for sh:

Let's assume we have previously defined variables like...

AWS_PROFILE=...
AWS_REGION=...
SG_ID=...


aws lambda list-functions \
    --profile "$AWS_PROFILE" \
    --region "$AWS_REGION" \
    --query "Functions[].FunctionName" \
    --output text | \
xargs \
    -n1 \
    -I{} \
    sh -c \
        "aws lambda get-function-configuration \
            --profile \"\$1\" \
            --region \"\$2\" \
            --function-name \"\$3\" \
            --query \"VpcConfig.SecurityGroupIds\" \
            --output text 2>/dev/null | \
        grep \
            -w \"\$4\" && \
        echo \
            \"Found in Lambda function: \$3\"" \
    _ "$AWS_PROFILE" "$AWS_REGION" {} "$SG_ID"


The sh -c command allows passing multiple arguments, which are referenced as $1, $2, $3, and $4 inside the shell script.

The underscore (_) is used as a placeholder for the $0 positional parameter inside the sh -c subshell.

When you use sh -c 'script' arg0 arg1 arg2 ..., the first argument after the script (arg0) is assigned to $0 inside the script, and the rest (arg1, arg2, etc.) are assigned to $1, $2, etc.

In this context, _ is a common convention to indicate that the $0 parameter is not used or is irrelevant. It simply fills the required position so that $1, $2, $3, and $4 map correctly to "$AWS_PROFILE", "$AWS_REGION", {} (the function name), and "$SG_ID".


References:

Sunday, 19 May 2024

Linux file-system permissions

 


Files and directories in Linux file-system have an owner who can set various permissions (e.g. read, write, execute) to the object's group owner or other system users. 

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 ls -la ./database_data/
total 128
drwx------ 19   999 root    4096 Apr 20 16:13 .
drwxrwxr-x  7 bojan bojan   4096 Apr 20 16:12 ..
drwx------  6   999 docker  4096 Apr 20 16:12 base


sudo ls -la ./database_data/ | awk '{print $3, $4, $9}'
999 root .
bojan bojan ..
999 docker base

$3 is user, $4 is group.

To change ownership (e.g. from root to current user) of directory and all its content use chown (change owner):

$ 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


User is the same as the owner.
Group is the user's primary group. 
Other/All/World refers to all other users.

Object owner can set permissions for the group or other users by using chmod command.


chmod with symbolic modes


chmod can have a format with symbolic modes:

chmod [u|g|o|a][+|-][r|w|x] <object>

u = user
g = group
o = other

plus (+) symbol adds a permission
minus (-) symbol removes a permission

r = read
w = write
x = execute

chmod u+r = "user plus read," as it gives the user read permission

chmod u-r  = "user minus read," as it takes the read permission away from the user.

chmod o-r,g-w = remove read permission from other, remove write permission from group

chmod go-rw,u+x file = give it execute permission to user, prevent anyone else from reading, writing, or executing it

The previous command can be written as:

chmod u=rwx,go= file

Note that there is a space after the second equals; this indicates a value of none.

The letter a is a shortcut to all users.

chmod a+rwx

is same as:

chmod ugo+rwx



chmod with modes as an octal number


chmod can also use a shorter version of representing permissions per subject - mode as an octal number. 

We have three subjects: owner, group and others. We have three permissions: read, write and execute and these can be combined. We can use a numeric representation for each permission like:

read = 4
write = 2
execute = 1
no permissions = 0

We can then represent e.g. read + write permissions as 4 + 2 = 6 or read + write + execute = 4 + 2 + 1 = 7. We can create such combination for each subject and we can introduce the rule that first number shows permissions for the owner, the second for the group and third for the others. This way we can represent permissions for all subjects as a 3-digit number e.g.
  • 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)
As the sum of these 4 allowed values (0, 1, 2 and 4) fits within range 0 - 7 we can say that this is actually a number in octal system so it's quite common that the permissions number string starts with zero like 0600, 0700, 0755...That leading 0 shows that the number is in octal system. 


To allow only file user  to read and write (but not to execute):

chmod 600 filename


It is very common to add executable permissions to all subjects for some bash script (apart from adding shebang #!/bin/bash at the beginning of the script so it is not necessary to call bash explictily):

$ chmod +x script.sh

If u, g or o is not stated before + or - then the new permissions apply to all subjects.

In the following example before chmod +x, the output of ls -la was:

-rw-rw-r-- 1 test_user test_user  300 Oct 30 15:54 download.sh

...and then after chmod +x:

-rwxrwxr-x 1 test_user test_user  300 Oct 30 15:54 download.sh



Typical permissions in Linux file-system



All directories in / should be 755 and files 655. 
/home/$USER (and subdirectories) should be 760 and files 640 or 650 if you want write permissions. An exception is /tmp which should be 777.


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.
Name comes from "www get".


To download a file from url [see Download Options (GNU Wget 1.24.5 Manual)]:

$ wget www.example.com/file.txt

If website has index file e.g. index.html, the following command will download it:

$ wget www.google.com
--2024-05-19 00:00:04--  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: ‘index.html’

index.html              [ <=>                ]  20.32K  --.-KB/s    in 0.01s   

2024-05-19 00:00:04 (1.89 MB/s) - ‘index.html’ saved [20807]


To set the custom name of the file into which a downloaded content will be saved into:

$ wget -O downloaded_file.txt www.example.com/file.txt
$ wget -O google_index.html www.google.com 

or

$ wget --output-document=downloaded_file.txt www.example.com/file.txt
$ wget -output-document=google_index.html www.google.com


If we want downloaded content only to be displayed in STDOUT (terminal), we can use "-" which is a pipe redirection to STDOUT:

$ wget -O - 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]

Dash (-) is often put straight after -O so we have -O-.

Mind the capitalisation:

-O file = outputs the downloaded content into the file or device (e.g. /dev/null)
-o file = outputs log information to file

If we don't need the command status information we can use this option:

-q = quiet; don't output status information

The following command will not print any status information and it will not save the downloaded content into the file:

wget -q -O /dev/null www.google.com


To show response headers, use -S:

$ wget -S "https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64" -O /dev/null
--2024-06-18 10:43:12--  https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64
Resolving code.visualstudio.com (code.visualstudio.com)... 13.107.253.64, 2620:1ec:29:1::64
Connecting to code.visualstudio.com (code.visualstudio.com)|13.107.253.64|:443... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 302 Found
  Date: Tue, 18 Jun 2024 09:43:13 GMT
  Content-Type: text/plain; charset=utf-8
  Content-Length: 162
  Connection: keep-alive
  Location: https://vscode.download.prss.microsoft.com/dbazure/download/stable/611f9bfce64f25108829dd295f54a6894e87339d/code_1.90.1-1718141439_amd64.deb
  Set-Cookie: MSFPC=GUID%3Deecbc0d63e8183c1315318e8090e8446%26HASH%3Deecb%26LV%3D202406%26V%3D4%26LU%3D1718703792920; Max-Age=31536000; Path=/; Expires=Wed, 18 Jun 2025 09:43:12 GMT; Secure; SameSite=None
  Vary: Accept
  Strict-Transport-Security: max-age=31536000; includeSubDomains
  request-context: appId=cid-v1:
  Content-Security-Policy: frame-ancestors 'self'
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  X-Powered-By: ASP.NET
  x-azure-ref: 20240618T094312Z-17ddf88f4d85c2wj3q7hcbzesg000000072g000000008114
  X-Cache: CONFIG_NOCACHE
  set-cookie: ASLBSA=00030be214d0cddc3ff4cc4fe91410644048dd542080144d9b14485a532831c2633c; Path=/; Secure; HttpOnly;
  set-cookie: ASLBSACORS=00030be214d0cddc3ff4cc4fe91410644048dd542080144d9b14485a532831c2633c; SameSite=none; Path=/; Secure; HttpOnly;
Location: https://vscode.download.prss.microsoft.com/dbazure/download/stable/611f9bfce64f25108829dd295f54a6894e87339d/code_1.90.1-1718141439_amd64.deb [following]
--2024-06-18 10:43:13--  https://vscode.download.prss.microsoft.com/dbazure/download/stable/611f9bfce64f25108829dd295f54a6894e87339d/code_1.90.1-1718141439_amd64.deb
Resolving vscode.download.prss.microsoft.com (vscode.download.prss.microsoft.com)... 152.199.21.175, 2606:2800:233:1cb7:261b:1f9c:2074:3c
Connecting to vscode.download.prss.microsoft.com (vscode.download.prss.microsoft.com)|152.199.21.175|:443... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Accept-Ranges: bytes
  Age: 58539
  Cache-Control: public, max-age=86400
  Content-Disposition: attachment; filename=code_1.90.1-1718141439_amd64.deb; filename*=UTF-8''code_1.90.1-1718141439_amd64.deb
  Content-Type: application/octet-stream
  Date: Tue, 18 Jun 2024 09:43:13 GMT
  Etag: "0x6DB5869CE33DCF08E5A02BCF311A662BEC363FBB7CC24B9D8FB4FE4790A0AFD1"
  Last-Modified: Tue, 11 Jun 2024 21:56:41 GMT
  Server: ECAcc (lhc/788C)
  X-Cache: HIT
  X-Ms-ApiVersion: Distribute 1.2
  X-Ms-Region: prod-neu-z1
  Content-Length: 102264142
Length: 102264142 (98M) [application/octet-stream]
Saving to: ‘/dev/null’

/dev/null                                    23%[===================>                                                                     ]  22.52M  3.31MB/s    eta 23s  



To delete the file after it is downloaded we can use the --delete-after option. 

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


It is possible to use redirection operators > and >> to achieve this:

  • > 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 “>>”?


Next two commands have the same output - if file.txt does not exist, it will be created and string test (without quotes will be written into it):

$ echo "test" > file.txt
$ echo test > file.txt
$ cat test.txt 
test


tee command


If we want command's output to be written in the terminal but also into a file, we can pipe the  command into the tee command which takes stdin and writes it to two outputs, one being a file and another stdout. tee will crate the file if it does not exist:

$ echo test | tee test3.txt
test
$ cat test3.txt 
test



tee command can also help when we need to write into a file that only superuser has write permissions. Our current bash terminal might be running as non-privileged user and so e.g. this command will fail:

echo line >> /etc/apt/sources.list

Using

sudo echo line >> /etc/apt/sources.list

...will not help as only echo will be executed as sudo.

The solution is:

echo line | sudo tee /etc/apt/sources.list > /dev/null

> /dev/null makes stdout being dumped to /dev/null.


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?

Difference between printf and echo:

printf "hello \n"
hello 
printf "hello " // note that new line is not appended automatically
hello $ echo "hello \n"
hello \n
echo "hello"
hello
$  // new line is appended automatically

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:

exiftool somefile.exe 

To install exiftool:

$ sudo apt install libimage-exiftool-perl

linux - viewing dll and exe file properties/attributes via the command line - Unix & Linux Stack Exchange

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 

gedit can also be used as graphic editor:

sudo gedit ~/.profile

To enter some special character (e.g. bulletpoint) press CTRL+SHIFT+U and underscored "u" should appear (u). Then use numeric keyboard to type in the Unicode code of the character (e.g. 2022) and press Enter. [source]

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:

$ find . -name "*STRING*" 

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
--include=\*.{c,h} =  search through those files which have .c or .h extensions
--exclude=*.o = exclude searching all the files ending with .o extension
--exclude-dir={dir1,dir2,*.dst} = exclude a particular directory(ies)
-e PATTERN = string pattern to be searched
-i = ignore the case

Example:

$ grep -r /var/lib/go/src/ -e "CodeDecode"
/var/lib/go/src/encoding/json/bench_test.go:func BenchmarkCodeDecoder(b *testing.B) {


Example:

$ 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:  myappnot 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.
...

To list all upgradable packages:

sudo apt list --upgradable

To upgrade all packages:

$ sudo apt upgrade

To see all installed packages:

$ sudo apt list --installed

To check if some package has already been installed:

$ sudo apt list --installed | grep package_name

...

If using Alpine distribution, you need to use apkComparison with other distros - Alpine Linux


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?  


Installing the .deb package will automatically install the apt repository and signing key to enable auto-updating using the system's package manager. Alternatively, the repository and key can also be installed manually.

Some applications are not available in Debian Package Repository but can be downloaded as .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?

Another example: Etcher

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



Installing binaries


Some applications are distributed as binary files. We need to download them and set executable permissions. Instead of using cp and chmod commands, we can use install command which copies file to destination directory and automatically sets -rwxr-xr-x permissions over the file:

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

$ sudo install minikube-linux-amd64 /usr/local/bin/minikube

$ ls -la /usr/local/bin/minikube
-rwxr-xr-x 1 root root 95637096 Apr 24 01:01 /usr/local/bin/minikube



We can pass to install command flags to set the owner and the group and also permission mode (as in chmod), instead of rwxr-xr-x:

$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

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) 

If you do not have root access on the target system, you can still install kubectl to the ~/.local/bin directory:

chmod +x kubectl
mkdir -p ~/.local/bin
mv ./kubectl ~/.local/bin/kubectl
# and then append (or prepend) ~/.local/bin to $PATH


It's worth noting here that install command can also just create a directory (all components of the path, like mkdir -p) and specify permissions on it:

$ install -m 0755 -d /etc/apt/keyrings


---

Thursday, 18 April 2024

Cron Utility (Unix)

cron command-line utility is a job scheduler on Unix-like operating systems. It runs as a daemon (background process).

These scheduled jobs (essentially a commands) are called cron jobs. They are repetitive tasks, scheduled to be run periodically, at certain time or interval.

Cron jobs, together with frequency and time of their execution are defined in cron table (crontab) file.
Each job is defined in its own line which has the following format:


minute (0–59)
# │ ┌───────────── hour (0–23)
# │ │ ┌───────────── day of the month (1–31)
# │ │ │ ┌───────────── month (1–12)
# │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
    *   *   *   *   *  <command to execute>


* means "every"

* * * * * = every minute, every hour, every day, every month
0 * * * * = every full hour, every day (HH:MM = *:0)
0 0 * * * = every midnight (HH:MM=0:0)
0 0 1 * * = once a month on the midnight of the first day of the month
0 10 * * * = every day at 10:00h
*/10 * * * * = every 10 minutes of every hour, every day


$ crontab
crontab: usage error: file name or - (for stdin) must be specified
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -T <file>  test a crontab file syntax
 -s         selinux context
 -V         print version and exit
 -x <mask>  enable debugging

Default operation is replace, per 1003.2


To list all cron jobs use:

$ crontab -l
* * * * * aws s3 sync ~/dir1/ s3://my-bucket/dir1 --region us-east-1 >> ~/logs/crons/s3_sync.log 2>&1
0 * * * * redis-cli -h redis-cache-group-123.cache.amazonaws.com -p 6345 flushall >> ~/logs/crons/flushRedisCache.log 2>&1
* * * * * ~/path/to/my_script1.sh >> ~/logs/crons/my_script1.sh.log 2>&1
0 0 * * * ~/path/to/my_script2.sh >> ~/logs/crons/my_script2.log 2>&1
0 0 1 * * ~/path/to/my_script3.sh >> ~/logs/crons/my_script3.log 2>&1
0 10 * * * ~/path/to/my_script4.sh >> ~/logs/crons/my_script4.log 2>&1
*/10 * * * * rsync -avhl --delete ~/path/to/source ~/path/to/dest/ >> ~/logs/crons/source_dest_rsync.log 2>&1

crontab file should not be edited with file editors but via crontab:

crontab -l


How to disable some cron job?


Simply comment its line in crontab with #.

How to disable all cron jobs?

Either comment all lines in crontab or 

$ crontab -l > crontab_backup.txt
$ crontab -r


-r = removes the current crontab 

To restore backup crontab:

$ crontab crontab_backup.txt
$ crontab -l


Resources:

cron - Wikipedia

Thursday, 11 April 2024

Linux Interview Questions

Here are some Linux Interview questions. Good luck!





Tuesday, 20 December 2022

Installing Node via Node Version Manager on Ubuntu


 
 
Node Version Manager allows you to install different versions of Node.js.
 
Installation instructions are here:

 
 We basically need to download and run a bash script:

$ 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

---