I need to understand command line syntax.

Xarzu

New Member
Joined
Apr 29, 2022
Messages
16
Reaction score
4
Credits
200
I confess, I have been using GUI interfaces for a few decades. I have gotten used to Microsoft Windows Operating Systems and I have not spend enough time navagating all of the command line commands.... and this is especially true for Linux distributions like Ubuntu.

I DO know what 'cd ..' does when I type it on a Ubuntu command line as well as 'ls'. And I know where '../Directory' will take me. It goes up one level in the folder structure and then back down to where ever the 'Director' folder is. So, if the folder I am currently is is called 'RightHere', then the command line entry of '../RightHere' will seemingly take me no where.

But I never learned what a single dot means. For example, where is the folder 'interest' in the command 'gzip -d ./interest/dino-mod.tar.gz'.

Also, I do not know how '&&' is used. What would the following line do:

'cd ../engine && mkdir thirdparty'

It looks like two commands. So why are they on one line? Is it to make them execute at the same time? Is one command dependent on the other? Where would the directory 'thirdparty" be made?

All of these are examples of what (sort of) appears in a .sh file I need to execute. I need to know these details to ensure that the directory paths align and are accurate to where I will execute the .sh file.
 


My best advice would be to buy a book on your preferred command line interpreter (bash, or zsh) and learn as if you were trying to learn to program.

Why a book? That's probably an advice from someone that tends to procrastinate and only make 1 in 20 useful web searches, and also from a rather aging Linux person: books capture your attention and are able to be consumed offline and away from distractions.

I assume that the line in your post is just an example, so I won't go into answering them.

Good books on shell scripting are the O'Reilly Unix series, and many times are on a Humble Bundle Books Bundle, which can get you a dozen books for about US $20.
 
@Xarzu there are a few things you can do with approach eg using pwd and ls


When you open a terminal in terms of "Location context: it opens up with itself being "present" in your home directory.

what output do you get with :

Code:
ls   .

the start of my output is :


Code:
[andrew@darkstar:~]$ ls .                                                 (05-13 09:44)
 abs/
 aurutils/
 bin/
 bookworm-git/
 brave-bin/
So "." means usually current directory whereas "../" means go up a directory

Code:
cd './RightHere

wouls only work if there was a Directory called "RightHere" at a level above your current directory.

You can find your present "location context" with
Code:
  pwd

Here is some light bed time reading for you https://tldp.org/LDP/abs/html/index.html


if you like books then : Bash in easy steps by Book by Mike McGrath
 
Hello @Xarzu,
You have received some good advise already.. But to answer your question concerning the
Also, I do not know how '&&' is used. What would the following line do:

'cd ../engine && mkdir thirdparty'
&& is use to tell the command to execute two command conseculativiely. In the example you used it would tell the command to first execute cd then mkdir in which case it would cd to the directory in question and then make a new directory thirdparty.
Another example you often see would be this sudo apt update && sudo apt upgrade In ubuntu or mint it would issue the command to update the package list and then preform a system upgrade.

I would second @arochester's recommendation to start with Linux Journey web page There command line tutorial walks you through the most commonly used commands. Enjoy the learning :)
 
Last edited:
I like "LearnLinuxTV" and "The Linux Experiment" Youtube channels, which have great beginner videos.

I also recommend this great book, "The Linux Command Line". Professional quality writing, and the PDF is free!
 
The recommendations people give here are great. I'd like only to add that one way to practice what you learn is by using a virtual machine, that way you won't risk damaging your system while experimenting with the things you learn in those books, videos and posts suggested here. You could use VirtualBox or QEMU for that. Run a Google search on those two, and decide which one to use based on your findings. :)
 
&& is use to tell the command to execute two command conseculativiely. In the example you used it would tell the command to first execute cd then mkdir in which case it would cd to the directory in question and then make a new directory thirdparty.
Another example you often see would be this sudo apt update && sudo apt upgrade In ubuntu or mint it would issue the command to update the package list and then preform an system upgrade.
I'd like to clarify something WRT the && (AND) operator:
&& only executes the command to the right, if the command to the left of the && completes successfully!

So in the case of @Xarzu 's example:
Bash:
cd ../engine && mkdir thirdparty

The above line attempts to cd (change directory) into ../engine. Which is a subdirectory in the parent of the current working directory.

What the && operator in the above does is:
If the cd operation succeeds (i.e. the directory at ../engine exists and the cd command was able to move into it and set it as the current working directory) - then the && operator will allow the mkdir thirdparty command to run. Which will create a sub-directory called thirdparty in the current working directory (which will now be the engine directory that we just moved up the tree into via the cd command).

But - if the call to cd ../engine fails.
For example if there is no engine directory in the parent (../) directory of the current working directory, or the user doesn't have appropriate permissions to enter the directory - then the && operator will not allow the mkdir operation to run because the previous command (cd) failed.

So && IS a way of stringing multiple commands together. But it's a conditional operator. It only runs subsequent commands if the previous commands succeeded.

If you want to specify multiple commands on the same line without caring about whether they succeed or fail, you would use semi-colons ;.
e.g.
Bash:
command1; command2; command3
That would run three commands sequentially, regardless of the success or failure of the command before the semicolon ;.
This is handy if you're performing a bunch of separate, distinct operations that are not related and do not depend on the success of another operation.

But there was a good reason for the OP to be using && in their example. Because there would be no point creating the thirdparty subdirectory if their ../engine directory didn't exist. So that is why && is used.

If they had used semi-colons ; to separate the commands and the cd command failed to find/enter the ../engine directory, then the mkdir command after the semi-colon ; would just create the thirdparty directory in whatever directory they were in before they issued the failed cd command. Which would not be what they were trying to do.

It's just a minor thing, but it's worth understanding!
 
I'd like to clarify something WRT the && (AND) operator:
&& only executes the command to the right, if the command to the left of the && completes successfully! ...
Good Post

They are ALL good posts. And some great advice for further resources.

Much thanks !
 
Since you're new, I recommend VirtualBox. No commandline to get it running. QEMU is a bit more advanced.
Just a tiny bit. And I've found that with QEMU Linux guests perform much better than with VBox.
 
Late to the party, but @kc1di , just be careful you give the full picture with that one, at #6

I was going to respond, but Jas did at #9 (and better than I, likely)

The point is, the use of a double ampersand (&&) makes the command conditional - right of the && will only go ahead provided that left of the && has worked.

One of the most common examples of this syntax is with

Code:
sudo apt-get update && sudo apt-get upgrade

in Debian-based distros.

Wiz
 

Members online


Top