Introduction
Today we will discuss on everything you need to know about Easy Linux Commands For Beginners in simple and easy to follow guide.
Did you know that you can automate all your tasks from a Linux Command Line?
Today I will break down three different ways on how to accomplish this task.
- Go over some searching capabilities
- Ways to automate tasks periodically
- Add some support to do file operations
I have been working in the Software industry for over 23 years now and I have been a software architect, manager, developer and engineer. I am a machine learning and crypto enthusiast with emphasis in security. I have experience in various industries such as entertainment, broadcasting, healthcare, security, education, retail and finance.
If you are beginner and now starting out with Linux this article is for you. If you are experienced and want to learn or see if you are missing some easy things to help you on your daily workflows please keep reading on too.
File System Commands
These commands are filesystem related and you need to master them in order to navigate Linux as a system. Also they are common among other Unix variants and their syntax is almost identical across the board.
CP
This is the copy command the syntax is as simple as cp <src> <dst> it can take folders so make sure you use the -r flag for recursiveness.
You can find the complete manual here.
MV
Similar as above the mv command lets you rename or move files and directories in your system use it as: mv <src> <dst>
You can find the complete manual here.
RM
RM lets you delete files or folders the syntax is: rm <file/folder> make sure to use -r for recursiveness and -f to specify the file a common usage would be like: rm -rf folder.
You can find the complete manual here.
LS
Probably the most important Linux command is ls. It lists the contents of the current folder directory and if given a destination it will list that instead. Use the -R if you want it to recursively list everything
I think before proceeding make sure you practise these and master them as it will give you an edge when using a terminal in Linux. The speed of using these commands over opening a UI version file management tool is much faster, especially when you are doing bulk operations.
Furthermore they are very useful if you are scripting things and want to execute things in batch jobs such as backups/archiving/mirroring files.
You can find the complete manual here.
GREP
The grep command lets you search the contents of a textual buffer. Usually it’s used as a conjunction of a pipe output as we will demonstrate below by examples. But it can also be used on it’s own to process a specific file. Lets dive into some examples and explain how it works.
Searching a file using grep
> grep <buffer> <file> > grep "Unbiased" names.txt Unbiased Coder
The above will search the name Alex in the file names.txt. One important thing to note here is that by default it’s case sensitive so if the name was alex it would not return anything. Instead you can tell grep to run with case insensitive using the -i argument. So the above command would be transformed as follows:
> grep -i alex names.txt Alex
Search the Output of Another Command Using GREP
alex@diamesos ~ > ls -al |grep Movies drwx------ 4 alex staff 128 Dec 4 03:44 Movies
The above command will get a listing of all the files in the filesystem and print out if the folder or file lola exists. As you notice we use the -i argument to it to tell it that we want our search to be case insensitive.
Negative search output from another command using grep
alex@diamesos ~ > ps auxww | grep -v init USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND alex 6541 9.3 1.7 61549604 577184 ?? S Tue09PM 63:04.00 /Applications/Signal.app/Contents/Frameworks/Signal Helper (Renderer).app/Contents/MacOS/Signal Helper (Renderer) --type=renderer --user-data-dir=/Users/alex/Library/Application Support/Signal --app-path=/Applications/Signal.app/Contents/Resources/app.asar --no-sandbox --no-zygote --field-trial-handle=1718379636,334234237892211478,16104510999879202599,131072 --disable-features=SpareRendererForSitePerProcess --lang=en-US --num-raster-threads=4 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --renderer-client-id=4 --no-v8-untrusted-code-mitigations --shared-files alex 10348 8.4 1.3 410572480 432080 ?? R 8:53PM 0:24.97 /Applications/iTerm.app/Contents/MacOS/iTerm2 ....
The above command lets you list all the running processes in the system with the exception of the init command or any command containing the string init. Since you can use regular exceptions to this you can use $ to see if the command ends with this string or ^ if the command starts with this string. For this you will want to use the parameter -E which tells grep to do a regex check. An example of that would be:
alex@diamesos ~ > ps auxww|grep -v -E "^1" USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND alex 6541 9.8 1.7 61549660 577220 ?? S Tue09PM 63:12.84 /Applications/Signal.app/Contents/Frameworks/Signal Helper (Renderer).app/Contents/MacOS/Signal Helper (Renderer) --type=renderer --user-data-dir=/Users/alex/Library/Application Support/Signal --app-path=/Applications/Signal.app/Contents/Resources/app.asar --no-sandbox --no-zygote --field-trial-handle=1718379636,334234237892211478,16104510999879202599,131072 --disable-features=SpareRendererForSitePerProcess --lang=en-US --num-raster-threads=4 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --renderer-client-id=4 --no-v8-untrusted-code-mitigations --shared-files alex 10348 6.2 0.7 410656240 247200 ?? R 8:53PM 0:30.51 /Applications/iTerm.app/Contents/MacOS/iTerm2
This will filter out any process starting with the number 1 since we are prefixing with the carrot symbol.
CAT
Another very useful Linux command is the cat command. The cat command lets you open and pipe to standard output or redirect to another file descriptor the contents of a file. For example lets say we want to see the contents of a file called names.txt we would run:
alex@diamesos ~ > cat names.txt Unbiased Coder Alex John Zafi
Pretty straight forward but you can also do this in devices which is useful if you want to do batch operations. For example if we want to get some random bytes into a file we can execute this command:
alex@diamesos ~ > cat /dev/random > random-data.txt ^C alex@diamesos ~ > cat random-data.txt <D4>I^]$<A5><BB><EA><D5><CD>t<EF><A4>^L<C4>^AESC<E7>P<BF><EE><85>R<9E><C0><DD><CD>4q<F7><F6>f<98><FE><83>]^@?^Y^D<DA>l<96>5<F0><D8>^<D3>^?<81><A6>E<BA>[L<A1>^H~ͣ<94><91>s <D9>z,<86><9E>l<BC><AF>r<EA><C6><D3>^U$<88><FA><9A><83><F9>ʾ^X<FD>3ҏ<AD>R<A4>Aو<DB>n<99><86>^MF<DD><FE>&\<80>o^_^N>^W{^E>{{<F4><B6><AB>w<B8>5A<C5>^Y0<A3>d<DC>4 ٚ<DF>^P'.^\<B9>Uy\^RY5^PƧ<9F><9E>^U^H<AB>^C<
As you can see the file rand0m-data.txt now has contents that are random directly from the device.
NETSTAT
The netstat command lets you see all the active connections on your computer. This includes basically every open connection you have in your browser and other apps. It also shows you what is listening on the machine for example if you have a web server running on port 80 it will show up as in the LISTEN state. If we execute this command on a vanilla Mac machine we can see all the active connections along with some listening services running on the computer.
alex@diamesos ~ > netstat -na Active Internet connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) tcp4 0 0 192.168.1.9.64546 1.2.3.4.443 ESTABLISHED .... tcp4 0 0 *.59663 *.* LISTEN tcp4 0 0 192.168.1.9.59656 1.2.3.4.443 ESTABLISHED tcp4 0 0 unbiased-coder.com.55767 unbiased-coder.com.55770 ESTABLISHED tcp6 0 0 *.5000 *.* LISTEN tcp4 0 0 *.7000 *.* LISTEN
The ones with the state LISTEN are basically services and the ones with ESTABLISHED are connections that are active right now. So lets say if I wanted to see if my chrome browser has made a connection to a server with the IP: 1.2.3.4 I can run the netstat command above and see if it’s there. From the output you can see it’s one of the active connections. This can be used for debugging among other informational needs like seeing if a service is running.
LSOF
LSOF is a similar command with netstat but does a lot more. For example we will start by simulating the behavior that we saw above where we want to see all network based file descriptors. As a reminder a file descriptor in Linux is not necessarily only a file it’s also a network socket. The below command lists all the network sockets that are IPv4 the -i argument followed by 4 indicates this.
Showing Open IPv4 Connections
alex@diamesos ~ > lsof -i4 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME loginwind 2788 alex 7u IPv4 0xb2067acb77cb397f 0t0 UDP *:* rapportd 2853 alex 3u IPv4 0xb2067acb81692c57 0t0 TCP *:51668 (LISTEN) rapportd 2853 alex 10u IPv4 0xb2067acb8462ac57 0t0 TCP 1.2.3.5:51668->1.2.3.4:52765 (ESTABLISHED) rapportd 2853 alex 11u IPv4 0xb2067acb77cb3c8f 0t0 UDP *:xserveraid WiFiAgent 2857 alex 6u IPv4 0xb2067acb77c0ad8f 0t0 UDP *:* identitys 2862 alex 25u IPv4 0xb2067acb77cb6d8f 0t0 UDP *:* ControlCe 2868 alex 20u IPv4 0xb2067acb78604197 0t0 TCP *:afs3-fileserver (LISTEN) ControlCe 2868 alex 22u IPv4 0xb2067acb7e97f6f7 0t0 TCP *:commplex-main (LISTEN)
The basic lsof command shows you everything such as:
- network file descriptors
- files
- pipes
- devices
- shared memory files
- cache files
Checking Open File Descriptors For /Users Files
An example below will show us all open files for the file path /Users. We leverage the grep command in combination with the lsof command to find out more.
alex@diamesos ~ > lsof |grep "/Users" com.apple 629 alex cwd DIR 1,17 384 244791 /Users/alex/Library/Containers/com.apple.geod/Data com.apple 629 alex txt REG 1,17 32768 297099 /Users/alex/Library/Containers/com.apple.geod/Data/Library/Caches/com.apple.geod/GEOConfigStore.db-shm com.apple 629 alex txt REG 1,17 32768 298282 /Users/alex/Library/Containers/com.apple.geod/Data/Library/Caches/com.apple.geod/AP.db-shm com.apple 629 alex txt REG 1,17 32768 299679 /Users/alex/Library/Caches/GeoServices/ReqCount.db-shm com.apple 629 alex txt REG 1,17 32768 317938 /Users/alex/Library/Containers/com.apple.geod/Data/Library/Caches/com.apple.geod/PDPlaceCache.db-shm ....
Conclusion
If you found Easy Linux Commands For Beginners useful and you think it may have helped you please drop me a cheer below I would appreciate it.
If you have any questions, comments please post them below I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.
Which Linux Command do you use the most?
I personally like to use grep. It’s my swiss army because it can be helpful when processing a lot of data or processing a lot of output without having to read through everything line by line. You may lose some information but you may also identify a bunch of things.
If you would like to learn more about automation you can take a look at some of my articles here: