- One command to create and change directory
- 9 Answers 9
- Linux — Create, Delete, Copy, and Move Files and Directories
- Filesystem Tree
- Absolute Path
- Current / Working Directory
- Changing Current (Working) Directory
- Creating Files and Directories
- Copy Files and Directories
- Move Files and Directories
- Deleting Files and Directories
- Listing Files and Directories
One command to create and change directory
I’m searching for just one command — nothing with && or | — that creates a directory and then immediately changes your current directory to the newly-created directory. (This is a question someone got for his exams of «linux-usage», he made a new command that did that, but that didn’t give him the points.) This is on a debian server if that matters.
I’m pretty sure the exam question was for you to understand why bash functions are necessary. Making a script won’t work (and is a bad answer).
This isn’t a good question for stackoverflow, either like @BasileStarynkevitch said he didn’t get the marks because he used a script and not function or he made some other syntactical mistake or it was a mistake with the marking/question itself. The only way you’ll find out is to speak to the exams markers/setters.
9 Answers 9
I believe you are looking for this:
Great answer, IMHO. I looked it up and $_ is a «Special variable set to final argument of previous command executed», source: tldp.org/LDP/abs/html/internalvariables.html
@BartvanKuik, . that said, the ABS isn’t a particularly good reference to point people to — it’s the W3Schools of bash, frequently using bad practices in its examples and rarely refreshed for outdated content. Consider instead wiki.bash-hackers.org/syntax/shellvars, or the official manual at gnu.org/software/bash/manual/html_node/Special-Parameters.html, or the BashGuide at mywiki.wooledge.org/BashGuide/…
define a bash function for that purpose in your $HOME/.bashrc e.g.
then type mkdcd foodir in your interactive shell
So stricto sensu, what you want to achieve is impossible without a shell function containing some && (or at least a ; ) . In other words, the purpose of the exercise was to make you understand why functions (or aliases) are useful in a shell.
PS it should be a function, not a script (if it was a script, the cd would affect only the [sub-] shell running the script, not the interactive parent shell); it is impossible to make a single command or executable (not a shell function) which would change the directory of the invoking interactive parent shell (because each process has its own current directory, and you can only change the current directory of your own process, not of the invoking shell process).
PPS. In Posix shells you should remove the function keyword, and have the first line be mkdcd()
Linux — Create, Delete, Copy, and Move Files and Directories
In this blog, we will see how to create, delete, copy and move files and directories in Linux.
Filesystem Tree
Linux organizes files and directories in what it calls the filesystem tree. Why is it called this way? Because just like a tree we’d see in nature, this also has a root, branches, and leaves. Except, Linux’s filesystem tree is inverted. The root is at the top, and its branches and leaves “grow” downwards.
The root directory is /. This is the top-level directory, there can be no other directories above it. Under / there are a few subdirectories like bin, etc, home, usr, and so on. These subdirectories may also contain other subdirectories themselves. To access a file or directory on our command line, we must specify its file path or directory path. This path can be written in two different ways:
Absolute Path
The easiest to understand is the absolute path.
/home/alex/Documents/Invoice.pdf
is an example of such a path.
Absolute paths always start out with the root directory /. Then we specify the subdirectories we want to descend into, in this case, first home, then alex, then Documents. We can see the subdirectory names are separated by a /. And we finally get to the file we want to access, Invoice.pdf.
An absolute path can end with the name of a file, but also with the name of a directory. If we’d want to delete the Documents directory, we’d specify a path like
Current / Working Directory
To understand a relative path we first have to explore what the current directory means. This is also called the working directory.
When we’re working at the command line, we’re always “inside” a directory. For example, if we log in as the user “alex” on some server, our starting current directory might be /home/alex. Every user starts in its home directory when they log in. john might have it at /home/john, and root (the super user/administrator) has it at /root.
To see our current (working) directory we can type
pwd = Print Working Directory
Changing Current (Working) Directory
To change our current directory, we use the cd command (change directory).
would change our current directory to /var/log. We used an absolute path here. But we can also change directory this way:
This will take us one directory UP.
If we were in /home/alex, this would take us into /home, which becomes the new current directory.
.. always refers to the parent directory of our current directory.
And this was an example of using a very simple relative path. Let’s dive deeper.
Let’s imagine our current directory is /home/alex. With relative paths, we can refer to other places in one of three main ways
- Locations “under” our current directory. E.g., Documents/Invoice.pdf Since we’re in /home/alex, typing a path like Documents/Invoice.pdf is like typing /home/alex/Documents/Invoice.pdf. Our relative path “gets added” to our current directory and we get to our PDF file.
- Locations in our current directory. Typing Invoice.pdf will access the file at /home/alex/Invoice.pdf
- Locations above our current directory. Typing ../Invoice.pdf points to the file at /home/Invoice.pdf. Since we used ../ we basically said, “go one directory up”. We can use .. multiple times.../../Invoice.pdf points to the file at/Invoice.pdf. The first .. “moved” the relative path at /home, the next .. moved it at /.
If you’re in /var/log currently and you move to / with
You can return to your previous working directory with
It will take you back to /var/log.
If you’re in /var/log and you want to return to your home directory /home/alex or /home/john or whatever it may be, use
cd without any options or paths after it will always take you back to the home directory.
Want to gain a deeper understanding of Linux’s main concepts? Watch this video.
Ok! Now let’s move on to the easy stuff.
Creating Files and Directories
To create a new file, type
This will create it inside the current directory. To create it at another location:
or, if we’re in /home/alex, we can use the relative path to create file in /home/jane:
All the commands we’ll discuss accept both absolute, and relative paths, so we won’t mention these alternatives for each one. Just know that after the command, you can use any kind of path you want.
To create a new directory:
Copy Files and Directories
To copy a file, we use the cp command, followed by the path to the file we want to copy (source), then the path to the destination where we want to copy it. “cp source destination”
To copy Invoice.pdf to the Invoices directory:
Notice how we terminated the path to the Invoices directory with a /, to make it Invoices/. “cp Invoice.pdf Invoices”, without the / would have worked too. But it’s good practice to end your directories with a /. This way, you’ll form a healthy habit and get a visual indicator that tells you when Invoices (without /) might be a file, and Invoices/ might be a directory.
To copy Invoice.pdf to the Invoices directory, but also choose a new name for it:
cp Invoice.pdf Invoices/InvoiceCopy.pdf
To copy the Invoices directory to the Documents directory:
The -r is a command-line option (also called command line flag) that tells cp to copy recursively. That means, the directory itself, but also descend into the directory and copy everything else it contains, files, other subdirectories it may have, and so on.
Just like we did with the file, we can also copy and rename:
cp -r Invoices Documents/BackupOfInvoices/
So instead of the copy ending up at /home/alex/Invoices, it will now end up at /home/alex/BackupOfInvoices
The name you choose for your cloned directory must not exist at your destination. For example, if we’d already have a directory at /home/alex/BackupOfInvoices, this would just move Invoices there and it would end up at Documents/BackupOfInvoices/Invoices/
Move Files and Directories
To move a file to the Invoices directory we use:
mv Invoice.pdf OldInvoice.pdf
To move the Invoices directory to the Documents directory:
Note that mv does not need the -r recursive option.
Deleting Files and Directories
Once again, the -r option was used to do this recursively, delete the directory, then all of its subdirectories and files. When you copy or delete directories, remember to always add the -r option.
Extra tip: if you want to quickly refer to your home directory without typing /home/alex or /home/john or whatever it might be, you can use the ~ sign. For example, let’s say you’re in /etc and you want to copy the file called fstab.
This is the same as typing
Wherever you put ~, it’s the same as typing the full path to your home directory, /home/alex.
If you’d want to copy it to /home/alex/Documents, you’d use:
Listing Files and Directories
To list files and directories in your current (working) directory
On Linux, files and directories can have a name that begins with a . Example: the “.ssh” directory. These won’t be displayed by a simple ls command. They are, in a way, hidden.
To list all files and directories, even the ones beginning with a .
-a comes from the word all.
Of course, to list files and directories from a different location, we just type the directory path at the end of ls.
To list files and directories in a different format, called a “long listing format”:
This shows us more details for each entry, like the permissions for a file or directory, what user/group owns each entry, when it was last modified.
We can combine the -a and -l command-line options like this:
This will display entries in long listing format and also show us “pseudo-hidden” files and directories which have a name beginning with a .
The last form is preferred as it’s faster to write it.
There’s also a command-line option, -h, that shows sizes in “human readable format”: bytes, kilobytes, megabytes, and so on. This has to be combined with the -l option. If we want to use three options like
-l
-h
-a
l, h, a can be typed in any order.
To learn more about Linux, check out our hands-on Linux course here
To get free hands-on experience on System Administration and DevOps tasks, please check out our KodeKloud Engineer program here