Search code snippets, questions, articles...

ubuntu code snippets

set default browser
xdg-settings set default-web-browser firefox.desktop
Changing User Password in Ubuntu Terminal
sudo passwd <username>      *username will not go into < >'s*
[sudo] password for <username>: 
New password: 
Retype new password: 
passwd: password updated successfully
Copy all folder content to another folder In Ubuntu
cp -a /path/to/source_dir/. /path/to/dest_dir/
Get total size of a directory or folder in Ubuntu
# If you are in same directory
------------------------------
du -hs

# If you are in different location
----------------------------------
du -hs /path/to/directory

# If you want to list the size seperatyly for each sub directory
----------------------------------------------------------------
du -h | sort -h
Delete all empty directories in a directory in Ubuntu
# Go to directory and execute command
-------------------------------------
find . -empty -type d -delete
Find all empty directories in a directory in Ubuntu
# Go to DIR and execute below command
--------------------------------------
find . -empty
--------------------------------------
>> It will list all the empty directories in the terminal
Extract, decompress or unzip a zip file in Ubuntu
# If unzip command not found
----------------------------
sudo apt-get install unzip

# To unzip the zip file on the same location
--------------------------------------------
unzip FILE_NAME.zip
>> It means the source and destination folders are same

# To unzip a zip file to a target location
------------------------------------------
unzip file.zip -d /path/to/destination_folder
Copy a directory along with its content to another directory in Ubuntu
# If directory does not exist in the target location
------------------------------------------------------
cp -r /path/to/directory /path/to/folder

# Copy a directory with a new directory name
------------------------------------------------------
cp -r /path/to/directory /path/to/folder/NEW_DIR_NAME
Remove or Delete a folder or directory in Ubuntu
# Delete a directory
------------------------------------------------
rm -rf DIRECTORY_NAME

# If show - Permission error
------------------------------------------------
sudo rm -rf FOLDER_NAME

# If not on the same level of the folder
------------------------------------------------
sudo rm -r /path/to/FOLDER_NAME
Change directory name or rename a directory in Ubuntu
# Rename directory on current path
----------------------------------------------
mv old_dir_name new_dir_name

# Rename directory using directory path
----------------------------------------------
mv /var/etc/old_dir_name /var/etc/new_dir_name

# Second method - gvfs-move command
----------------------------------------------
gvfs-move /home/user/old_dir_name /home/user/new_dir_name
Create a new directory or folder in Ubuntu
# create new directory(in current folder)
-----------------------------------------------
mkdir directory_name
-----------------------------------------------

# To verify it---
-----------------------------------------------
ls -l
-----------------------------------------------

# create directory on a path(another location)
-----------------------------------------------
mkdir /path/to/folder/directory_name
-----------------------------------------------

# create directory along with it parent directory
-----------------------------------------------
mkdir -p /home/new_parent_direcory/directory_name
-----------------------------------------------
>> If we use -p with mkdir it will create the parent direcotry also if it does not exist