Linux

| 4 min read

2021-05-23

Linux

home/ Professional

Common Commands

  1. To run some command in the background(Dockerd):
sudo dockerd &
  1. To run xrdp (Remote desktop to the WSL):
sudo systemctl status xrdp

If you want you can install Linux GUI

yum/dnf

for query some package in distributions like Oracle linux or Centos:

sudo dnf repoquery "*libunwind*"

if you want the entire list of packages:

sudo dnf repoquery "*"

apk

in Alpine distro you can install (except of apt-get):

apk add git

curl

curl is the command for sending an http-request and trecking it curl

find

command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.

$ find [where to start searching from]
 [expression determines what to find] [-options] [what to find]

Example from life:

find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full

type f means that the input type is a file.
-empty This command find all empty folders and files in the entered directory or sub-directories.
More About find command

alias

If I've some dir for some program that I want to initiate with ease I can run this program:

alias sqlcl="c:/sqlcl/bin/sql.exe"

adduser, usermod

  • Create a new user named marlena, run: adduser marlena
  • Make marlena user ‘sudo user’ (admin) run: usermod -aG sudo marlena

File permissions: How to change directory permissions in Linux | Pluralsight

grep

The grep command is a great tool for searching. Let’s try with a simple pattern matching:

$ grep -q "apple" <<< "pineapples" && echo "Found it with grep" 
Found it with grep

Here, we’re using a here-string, “<<<“, to feed “apple” into grep. Additionally, we can use a pipe to avoid the bashism:

$ echo "pineapples" | grep -q "apple" && echo "Found it with grep"
Found it with grep

In both cases, we used the parameter -q because we want only the exit status.

find Substring in some string

The shells have several tools that can help us to perform string evaluations. For example, the Bash shell has the [ ] builtin commands and the  keywords.

These tools can perform string evaluations that we can use to search for a string inside another.

title: For examples
collapse: closed

Let’s say we want to find the substring “apple” within the string “pineapple”, with the help of the  keyword and the = operator:

$ <a class="internal-link" href="/and-quot-pineapples-and-quot-and-quot-apple-and-quot"> "pineapples" = *"apple"* </a> && echo "Found it :)"
Found it :)

Here, we’re instructing the shell that the string to the right of the = operator will be considered a pattern. The special character “*” surrounding the word “apple”, will match any string.

Similarly, we can use the =~ operator:

$ <a class="internal-link" href="/and-quot-pineapples-and-quot-and-quot-apple-and-quot"> "pineapples" =~ "apple" </a> && echo "Found it again :)"
Found it again :)

In this scenario, we instruct the shell that the string to the right of the =~ operator will be considered a POSIX extended regular expression. Therefore, this expression will search for a match within the word on the left.

Next, let’s try with the builtin command [ ], to avoid bashisms and make it more portable:

$ [ -z "${pineapples##*apple*}" ] && echo "Found it again :)"
Found it again :)

In this scenario, we use the parameter expansion to remove everything that matches the “apple” substring. If the match is successful, then the result will be empty, and the exit status of the test [ -z … ] will be true.

Finally, we can use the case keyword:

$ case pineapples in 
    *apple*) echo "Found it :)";; 
    *) echo "Not Found";; 
  esac
Found it :)

In this example, we’re performing a pattern matching on the “pineapples” string. If the string “apple” is matched, the pattern between the delimiter “;;” will be executed.

for file in $(fgrep -rnwl "dafyomi" $(pwd)); do mv "$file" "$(pwd)/dafyomi"; done

while :

  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

shutdown or restart

Restart immediately:

shutdown -r now

Restart in 5 minute:

shutdown -r +5

Restart at 5:00 pm:

shutdown -r 17:00

permission

To change the permission of the file or directory, for all users(a) you can change it by

chmod a+rwx connection.sh

for the owner only it will be u+rwx, for the group g+rwx and others o+rwx.

to see the permission state:

ls -l

!Drawing20220808144502- Linux File Permissions.svg
If you want to let the owner all permission (7) and to groups write+execute(4+1) ant to all users only read(4) you can do:

chmod 764 connection.sh