By Astrea, 19 January, 2024

Useful Terminal commands 
Just to make life easier when using the terminal in Linux.

 

Print Working Directory.
pwd  will display your current directory ei. /home/username 
 
Concatenate. 
Can be used to show you the content of a file.
cat filename

Concatenates the contents of the specified file to the end of a new file.
cat file > newfile
pres ctl + d when you are done.

Copy
copy filename.txt to Documents folder. 
cp filename.txt /home/username/Documents 
 
-r directory1 directory2
copies the directory directory1 and all of its contents to a new directory named directory2 in the current directory. 

Move files or rename them.
mv filename.txt /home/username/Documents 
This will move filename.txt to Documents.

mv directory1 directory2  
Moves the directory directory1 to a new directory named directory2 in the current directory.
 
Make Directory.
The mkdir command is a essential tool for organizing your files and directories. 
 
Create new directories in the current directory.
mkdir mydirectory  

Creates 3 directories in the current directory ( Documents , images, videos )

mkdir documents images videos

Creates a new directory named mydirectory with two subdirectories named subdir1 and subdir2

mkdir -p mydirectory/subdir1/subdir2

Remove Directory(use with caution).

Remove the empty directory named mydirectory in the current directory.
rmdir mydirectory
 Remove the empty directory subdirectory1 within the directory1 directory
rmdir -p directory1/subdirectory1

 Directory.

dir Lists the contents of the current directory.

The dir command is a good alternative to the ls command, which is the more common command for listing directory contents in Linux. The dir command produces a more readable output than the ls command, especially when displaying long filenames and file permissions.

Change Directory
cd ~/Documents
changes directory to Documents 

cd /home/user/Documents/Project - Changes to the Project directory in the Documents directory.

cd .. - Changes to the parent directory of the current directory.

Change to the previous directory.
cd -  
 

Global Regular Expression Print.
The grep command is a powerful tool for finding text patterns in files. It is a versatile command that can be used for a variety of tasks, and it is an essential tool for any Linux user.

grep "Hello, world!" filename.txt
Searches for the exact phrase "Hello, world!" in the file filename.txt
grep -r "error" /path/to/directory
Searches for the text "error" in all files in the directory named /path/to/directory and its subdirectories.

clear 
The clear command is a useful tool for keeping your terminal organized and preventing clutter. It can be used to start a new session, clear up after a complex command, or simply make the terminal look more appealing.


Touch 
touch filename.txt  - Creates an empty file named filename.txt in the current directory.

Chown - Change ownership of files and directories.  Allows you to set the owner and group for files and directories.
Sudo chown your_username:Your_group /dir/folder_or_filename
 

ln -  Symbolic link  is a type of file that points to another file or directory. Useful when setting up Web servers or FTP servers. To create a symbolic link named shortcut that points to a directory
ln -s /path/to/directory shortcut

To remove a symbolic link, you can use the unlink command or rm.

unlink shortscut
rm shortcut


 

Tags

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.