Categories
Tools

Useful Linux Commands — Users, Groups, and Remote Connections

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

printf

printf is an improved version of the echo command.

It lets us format and adds escape sequences:

printf "1\n3\n2"

We can also use it with < to get input from it and sort it with sort :

sort <(printf "1\n3\n2")

0 / 1 / 2

0, 1, and 2 are the standard input, output, and error streams, respectively.

For instance, we can redirect to stdout with:

echo "stdout" >&1

And we can redirect to stderr with:

echo "stderr" >&2

users

The users command shows all the users currently logged in.

To see all users on the system, we can check /etc/passwd .

useradd

To add a user to the system, we run useradd .

For instance, we run:

sudo useradd foo

to add the foo user.

userdel

userdel lets us delete the user.

For example, we run:

`sudo` userdel `foo`

to delete the foo user from the system.

groups

groups show all the groups of which the current user is a member.

We can see all groups in the /etc/group .

We shouldn’t change /etc/group unless we know what we’re doing.

groupadd

groupadd lets us add groups into our system.

We can run:

sudo groupadd foo

to add the foo group.

groupdel

groupdel lets us delete a group.

We can run:

sudo groupdel foo

to delete a group.

cmp

cmp gets the byte difference between 2 files.

For instance, we run:

cmp a b

to get the byte difference between files a and b .

cut

cut lets us cut a line into sections on some delimited.

The -d flag lets us specify a delimiter.

-f specifies the field index to print.

For instance, we run:

printf "117.99.234.23" > c
cut -d'.' c -f1

to print 117 onto the screen.

sed

sed lets us replace a string with another string in a file.

For instance, we runL

echo "old" | sed s/old/new/

to remove old with new .

ssh

ssh lets us connect from one machine to another machine.

We run:

ssh –p <port> bob@1.2.3.4

to connect to machine with IP address 1.2.3.4 with user bob .

scp

We can use scp to copy a file securely from one machine to another.

For instance, we can run:

scp –P <port> hello bob@1.2.3.4:~

to copy the hello file from the machine with IP address 1.2.3.4.

rsync

The rsync utility lets us copt files with the least amount of data possible being transmitted.

This is done by looking at changes between files.

For instance, we run:

rsync -av ../s/* .

to sync the files from the ../s directory with the current directory with rsync .

rsync also works over ssh .

We run:

rsync -avz -e "ssh -p <port>" bob@1.2.3.4:~/s/* .

to copy the data over to the machine with IP 1.2.3.4.

Conclusion

We can print files, manipulate users and groups, and connect to other machines with some Linux commands.

Categories
Tools

Useful Linux Commands — Files and Packages

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

head

The head command outputs the first few lines of a file.

The -n flag specifies how many lines to shows.

The default number of lines to show is 10.

tail

tail outputs the last few lines of a file.

We can also use the -n flag to specify how many lines to show.

Also, we can get the end of the file beginning with the N -th line with:

tail -n +N

cat

cat concatenates list of files and send them to the standard output stream.

less

less is a quick tool to let us view a file.

It opens up the file content with a read-only window.

nano

nano is a small text editor.

It’s easy to use for beginners since we don’t have to learn lots of shortcuts.

nedit

nedit is a small graphical rextr editor.

And it lets us edit text with point and click, drag and drop, and syntax highlighting.

touch

touch lets us modify the timestamp of an existing file and quickly create a new file.

logout

logout lets us exit the shell we’re logged into.

ncdu

ncdu lets us display file space usage.

It opens a window that shows the disk usage of each file.

top

top displays all currently running processes and their owners, memory usage, and more.

htop

htop is an interactive version of top .

We can pass -u username to display processes that are owned by user username .

whereis

whereis lets is search for files related to a particular command.

For instance, we run:

whereis ls

to find the path to the ls command and the associated manpage.

whatis

whatis prints the description of the command from its man page.

locate

The locate command finds a file anywhere in the system by searching a cached list of files.

find

find iterates through the file system to find the file we’re looking for.

It looks at files that currently exist in the system.

find lets us search by file age, size, ownership, type, timestamp, permissions, depth within the file system, regex, and more.

wget

wget lets us download a file from the Internet

curl

curl can be used like wget , but we need the --output flag.

curl supports many moe protocols and it’s more widely available that wget .

wget can only receive data, but curl can also send data.

wget can download files recursively, but curl can’t.

apt

The apt command is available in Debian based distros.

It can be used to install, upgrade, or delete software on our machine.

We can run apt search to search for packages. apt install lets us install packages.

Conclusion

Linux distros comes with many commands that we can use to manage files and download files.

Categories
Tools

Useful Linux Commands — File Operations and Multiple Commands

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

less

The less command lets us show the content of a file.

The general syntax is:

less <filename>

We can navigate with arrow keys.

Spacebar and b navigates page by page.

/ lets us search for content.

? search backward.

F enters follow mode. When the file is changed, the changes are displayed live.

ctrl+c quits follow mode.

cp

cp lets us move files and folders.

For instance, we run:

cp foo bar

to copy foo to bar .

We can also use it to copy folders with the -r switch:

cp -r fruits cars

We move the fruits folder contents to cars .

mv

The mv command lets us move files and folders.

For instance, we run:

mv foo bar

to move foo to bar .

We can move files into a folder with:

mv grape banana fruits

We move the grape and banana files to the fruits folder.

ls

The ls command lets us list files and folders.

We can list the content of a given folder with:

ls /bin

We can add the a switch to show hidden files.

l shows files permissions, file and folder sizes, and modified date time.

We run ls -al to show all that info.

rmdir

The rmdir command lets us remove a folder.

We run:

rmdir fruits

to remove the fruits folder.

We can use it to remove multiple folders:

rmdir fruits cars

We remove the fruits and cars folders.

The folders we delete must be empty.

To delete non-empty folders, we run:

rm -rf fruits cars

-r means recursive and f means force.

pwd

pwd shows the current working directory.

cd

cd lets us change the current working directory.

For instance, we run:

cd fruits

to go to the fruits folder.

cd .. to move to the home folder.

cd ../cars moves to the parent folder of cars .

We can use absolute paths with cd , so we run cd /etc to go to the /etc folder.

mkdir

mkdir lets us create a folder.

For instance, we run:

mkdir cars

to create a cars folder in the current working directory.

And we run:

mkdir dogs cars

to create the dogs and cars folders.

We can create multiple nested folders with the -p switch:

mkdir -p fruits/apples

!!

!! lets us run the last command.

; / && / &

; lets us run one command after the other like:

ls; pwd

&& runs multiple commands but the ones on the right won’t run if the left one fails.

& lets us run multiple commands in parallel instead of waiting for the current one to finish before running the next one.

Conclusion

We can run commands to show file content and run multiple commands with some operators with Linux.

Categories
Tools

Useful Linux Commands — Soft Links and Files

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

Soft Links

We can create soft links with the ln -s command.

The general syntax is:

ln -s <original> <link>

Soft links will be broken with the original file removed.

For instance, we run:

ln -s foo.txt newfoo.txt

to create the newfoo.txt soft link.

We can see that the soft link should have th @ suffix and it’s colored differently when we run the ls -al command.

find

The find command lets us find files and folders on the file system.

For instance, we can find all files with the .txt extension by running:

find . -name '*.txt'

We need quotes around special characters like * to stop the shell from interpreting them.

We can also he it to find directories with the -type d switch:

find . -type d -name src

-type f lets us search for files only.

-type l lets us search only symbolic libks.

We can also search under multiple root trees:

find folder1 folder2 -name foo.txt

We can also search with multiple keywords with -or :

find . -type d -name node_modules -or -name public

We can exclude results with -not :

find . -type d -name '*.md' -not -path 'node_modules/*'

And we can search for files with at least with the given size with -size :

find . -type f -size +100c

c means bytes.

We can search for files with size in a range with:

find . -type f -size +100k -size -1M

We can search for files that are edited more than a given number of days ago with -mtime :

find . -type f -mtime +3

And we can delete files that are found with the -delete option:

find . -type f -mtime -1 -delete

cat

cat lets us add content to a file.

To print content to standard output, we run:

cat file

To print the content of multiple files, we run:

cat file1 file2

We can redirect the output to a file by running:

cat file1 file2 > file3

We can change > to >> top create the file if it doesn’t exist.

And we can print line numbers with -n :

cat -n file

touch

The touch command lets us create an empty file.

For instance, we run:

touch file

to create a file named file .

If it already exists it opens the file in write mode and the timestamp is updated.

tail

The tail command lets us show the content at the end of a file,

The -f switch watches for file changes and updates the output automatically.

For instance, we run:

tail -f /foo.txt

to watch foo.txt and update the output.

We can change the number of lines printed with -n :

tail -n 10 <filename>

We print the last 10 lines with -n 10 .

We can print the whole file content starting from a specific line with + before the line number:

tail -n +10 <filename>

Conclusion

We can create soft links and create and manipulate files with Linux commands.

Categories
Tools

Useful Linux Commands — Processes and Links

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

kill

The kill command lets us send signals to a process.

The general format is:

kill <PID>

where PID is the process ID.

We can send signals like:

kill -HUP <PID>
kill -INT <PID>
kill -KILL <PID>
kill -TERM <PID>
kill -CONT <PID>
kill -STOP <PID>

HUP means hang up. It’s sent when a terminal window that started a process is closed before terminating it.

INT means interrupt. It sends the signal when we press ctrl+c.

KILL is sent to the OS kernel to stop and terminate the process.

TERM means terminate. The process that receives it will terminate.

CONT means continue. It lets us resume a stopped process.

STOP is sent to the OS kernel instead of the process, which stops but doesn’t terminate the process.

We can also use a number to replace these signals. 1 is HUP , 2 is INT , 9 is KILL , 15 is TERM , 18 is CONT .

top

The top command lets us list processes running in real time.

We can quit top with ctrl+c.

And we can sort processes by the amount of memory used by running:

top -o mem

echo

echo lets us print arguments passed to it.

For instance, we run:

echo "hello" >> output.txt

to put hello into the output.txt file.

Also, we can interpolate environment variables in the string:

echo "path=$PATH"

We have to escape special characters like $ in the command.

We can echo the files in the current folder with:”

echo *

And we can echo the files that start with a with:

echo a*

We can print the home folder path with:

echo ~

We can also print the results of a command with $() like:

echo $(ls)

ps

The ps command lets us list the processes currently running in the system.

We can list all processes with the ps ax command.

a is used to list other users’ processes.

x shows processes not linked to any terminal.

ps axww continues the command listing on a new line instead of truncating it.

The output has the PID which is the process ID, TT tells us the terminal ID used.

STAT tells us the state of the process.

I means the process is idle. R is a runnable process. S is a process that’s sleeping for less than 20 seconds.

T is a stopped process, U is a process in uninterruptable wait and Z is a dead process.

+ means the process is in the foreground. s means the process is a session leader.

ln

ln lets us create links in the filesystem.

The syntax of the command is:

ln <original> <link>

For instance, we run

ln foo.txt newfoo.txt

to create the newfoo.txt link to the foo.txt file.

Links are indistinguishable from a regular file from the user’s perspective.

Conclusion

We can manage processes and create symbolic links with Linux commands.