-
ls
: Lists files, directories in the current directory.Example:
ls -l
lists all details for every item. You can see hidden items as well with this command. -
cd <directory_name>
: Changes your working directory to<directory_name>
.If you want go up one level (or parent) folder, use
..
-
pwd
: Prints the path of current working directory.Example:
pwd
prints out/home/user/documents/
. -
mkdir <new_directory_name>
: Creates a new directory with specified name.To create nested directories in single command you can specify names like this -
mkdir dir1/subdir2
-
rm <file/directory>
: Removes files or folders. You must be very careful while using it as deleting is
permanent.Example:
rm filename.txt
deletes a text file with given name.To delete non-empty directories, use recursive option like this -
rm -r directoryname/
. -
touch <file_name>
: Creates an empty files or updates timestamps for existing ones in current working
directory. You can create as many new files using one command separated by spaces.Example:
touch file1.txt file2.txt
-
Copying and moving operations:
-
cp source_file destination_folder
: Copies a single specified file to the given folder.If you want multiple copies of same or different sources, specify them like this -
cp -r /source/folder/. /destination/folder/
.
To move files from one place to another use following command:
Example:
mv source_file destination_folder
-
-
cat <file_name>
: Shows content inside the specified file. You can see any type of data. -
Displaying all lines in a single line, instead of new lines between them, add option like this -
cat filename.txt | tr "\n" " "
.Example:
cat sample1.txt sample2.txt
-
Searching for files or strings:
Using grep command to search contents within multiple text files can be done as follows:
- Syntax :
grep 'search_string' *.txt
In this case, all the txt filenames present in current working directory will be searched.
Example: ls | grep '.sh'
- List of .bashrc files found using above command.
- Using pipes :
Piping commands together allows for chaining processes. You can see below examples:
-
ls | wc
: Counts total number lines printed by ‘ls’.The first part, ‘ls’, prints all the contents in current directory.
Second one counts them as shown here - wc -l
.
- Modifying permission using chmod command:
Permissions can be changed for users, groups and others. You must have appropriate permissions to change any
file/folder’s rights.
Syntax : chmod options <permissions> filename
- Changing ownership with chown:
To transfer full control over a given folder/file you need root privileges.
Example - chown user1:user2 test.txt
.
- Checking disk usage and free space in Linux:
You can check total size, used storage etc. by running following command- du .
-
Creating text files with echo :
This will display a specified message inside given filename.
Example - echo "Hello World" > test.txt
.
- Displaying contents of terminal window:
You can scroll back up and down using cursor keys while you are in any command prompt.
To exit current session, type ‘exit’ or press the keyboard shortcut Ctrl+D.
- Working with directories:
List files/directories including hidden items by typing ls -a
.
- Changing directory : Use cd to move between different folders.
Example- cd documents/
You can go back up one level or parent folder using ‘..’
To get your current working directory use pwd
command.
- Creating new directories:
New empty folder is created with mkdir <dir_name>.
For nested/new sub-directories you must specify them like this- mkdir dir1/subdir2
.
- Deleting files/directories :
Remove a single specified file using rm filename.txt.
To delete non-empty directories use recursive option -rm -r directoryname/
- Creating new empty text/file/folder:
Create as many copies of same or different sources with one command separated by spaces. cp source_file destination_folder
.
- Copying and moving files/directories:
To move all your work from ‘documents’ folder to another existing directory, use this- mv documents/* /home/user/
- Searching for strings/file using grep command:
Syntax : grep search_string *.txt
Searches contents within multiple text files. All the .txt filenames in current working dir will be searched.
Example - ```
ls | grep ‘.sh’`` - List of bashrc files found above.
- Displaying all lines inside a single line with no newline character using cat command:
Syntax : cat filename.txt| tr "\\n" " "
You can specify multiple filenames separated by spaces as well: cat file1.sh file2.sh | sed 's/ /\\\//g'
- Modifying permission using chmod command:
Change permissions for users, groups and others. Make sure you have required rights to do so.
Syntax : chmod options <permissions> filename
- Changing ownership with chown:
Transfer control over a folder/file by changing owner with root privileges.
Syntax - chown user1:user2 test.txt
.
- Checking disk usage and free space in linux:
You can check total size, used storage etc. using this command- du .
- Creating text files with echo:
Display a specified message inside given filename.
Example - echo "Hello World" > test.txt
.
- Viewing contents of terminal window:
You can scroll up and down while you’re in any command prompt using cursor keys. Exit current session by typing
‘exit’ or pressing Ctrl+D.
- Working with directories:
List files/directories including hidden items - ls -a
.
Move between different folders- use cd to change directory.
Go back up one level/folder with ‘..’.
Current working dir can be checked using pwd
command.