Command Line Basics: Difference between revisions
creating basic linux commands the right way |
No edit summary |
||
| Line 1: | Line 1: | ||
This page serves as an introduction to basic commands for managing your file system on Linux. | This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics. | ||
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers. | |||
=== Creating a Directory === | |||
To create a new directory, you can use the <code>mkdir</code> command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create:<syntaxhighlight lang="linux-config"> | |||
mkdir MyDirectory | |||
</syntaxhighlight> | |||
=== Moving Between Directories === | |||
The change directory command is called <code>cd</code> and can be used to efficiently switch between directories. To move into our newly created directory, type:<syntaxhighlight lang="linux-config"> | |||
cd MyDirectory | |||
</syntaxhighlight>If we want to go backwards, we can use <code>..</code>, which always means to go back one folder from where you currently are.<syntaxhighlight lang="linux-config"> | |||
cd .. | |||
</syntaxhighlight>If we want to return to our root directory, we can simply type the command with nothing after it.<syntaxhighlight lang="linux-config"> | |||
cd | |||
</syntaxhighlight>I can check my location using the present working directory command or <code>pwd</code>. Typing <code>pwd</code> into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root. | |||
=== Moving and Copying Files === | |||
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:<syntaxhighlight lang="linux-config"> | |||
mv myfile MySubDirectory/ | |||
</syntaxhighlight>The first thing after <code>mv</code> represents the file to move, and the second represents the destination. <code>mv</code> is also good for renaming files. I can change the name of myfile to mynewname as follows:<syntaxhighlight lang="linux-config"> | |||
mv myfile mynewname | |||
</syntaxhighlight>This just renames the file in place without moving it elsewhere. | |||
If I have a directory, I might want to move its entire contents to a new directory. This requires the <code>-r</code> flag, meaning recursive. This essentially means that for every element in the directory, we recursively move those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use.<syntaxhighlight lang="linux-config"> | |||
mv -r MySubDirectory ../MyNewDirectory/ | |||
</syntaxhighlight>This code recursively moves everything that is in MySubDirectory into MyNewDirectory, which is located one folder back from our current location in the terminal. | |||
Sometimes, we don't want to move files but we do want to copy them. This is the <code>cp</code> command, and it works much the same way as the <code>mv</code> command. To use <code>cp</code>, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.<syntaxhighlight lang="linux-config"> | |||
cp myfile ../ | |||
</syntaxhighlight>This creates a new copy of myfile in the previous directory on the path to my current working directory. We can also copy over the entire contents of a folder using the recursive flag.<syntaxhighlight lang="linux-config"> | |||
cp -r MyDirectory ../ | |||
</syntaxhighlight> | |||
=== Deleting Files === | |||
Revision as of 19:22, 10 July 2025
This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics.
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.
Creating a Directory
To create a new directory, you can use the mkdir command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create:
mkdir MyDirectoryMoving Between Directories
The change directory command is called cd and can be used to efficiently switch between directories. To move into our newly created directory, type:
cd MyDirectoryIf we want to go backwards, we can use .., which always means to go back one folder from where you currently are.
cd ..If we want to return to our root directory, we can simply type the command with nothing after it.
cdI can check my location using the present working directory command or pwd. Typing pwd into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.
Moving and Copying Files
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:
mv myfile MySubDirectory/The first thing after mv represents the file to move, and the second represents the destination. mv is also good for renaming files. I can change the name of myfile to mynewname as follows:
mv myfile mynewnameThis just renames the file in place without moving it elsewhere.
If I have a directory, I might want to move its entire contents to a new directory. This requires the -r flag, meaning recursive. This essentially means that for every element in the directory, we recursively move those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use.
mv -r MySubDirectory ../MyNewDirectory/This code recursively moves everything that is in MySubDirectory into MyNewDirectory, which is located one folder back from our current location in the terminal.
Sometimes, we don't want to move files but we do want to copy them. This is the cp command, and it works much the same way as the mv command. To use cp, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.
cp myfile ../This creates a new copy of myfile in the previous directory on the path to my current working directory. We can also copy over the entire contents of a folder using the recursive flag.
cp -r MyDirectory ../