I have horrible memory when it comes to names and numbers, same is true for Linux commands. I use and then quickly forget useful commands or switches. I always promises my self to note down these commands but its not working. This blog entry (and the future versions) will serve as personal manual.
Show installed packages (all)
Search for a specific package e.g. gdal
dpkg -l | grep gdal
What if I want to know if a package is installed or not.
for example, package gdal is installed or not (Note you have to provide full package name, its not like grep)
dpkg-query -l libgdal-dev
or just the -S switch
dpkg -S libgdal-dev
and if you want to know dependencies of a package, run the following command
apt-cache showpkg package_name e.g. apt-cache showpkg libgdal-dev
Note: Not only the above command shows package dependencies for libgdal-dev package but will also show packages depending on libgdal-dev,see section Reverse Depends in the output.
List files, sorted by creation date and time
Sort by file size
List hidden folders
ls -ld .?*
List folder with permissions
ls -ld */
Locate command is dependent on a DB, thus, it is necessary to run the following command from time to time (use sudo)
Locate command is perfect but some times I want to find folder under a specific path
find /path -name "name" -type d
find /var -name "apache2" -type d
-type d is for directory
and to find out folder size
switch h is for human readable format and s is summarized.
Create multiple folders with one command (the command below will create folder 1990, 1991….2010)
Delete folders (and files in them)
rm -rf foldername or rmdir foldername
-f is for force (you will not be asked again and again for ‘yes’)
File size using the following command
h is for human readable format.
Copy files from one location into other
cp -v file*.html /var/www/website
Note: Use -r switch to copy directories.
copy all files from one folder to an other
cp Folder1/* Folder2/
mv filename folder_name Example: mv test.png /var/www/images or for folder mv -r /home/user/folder /location/to/cop
mv original.txt renamed.txt
sudo might be required
Cat command is one of Linux powerful commands. It sequentially concatenates multiple files and can be redirected into a file or pipe for an other command.
cat *.txt > merged-file.txt
cat *.txt > grep "some search string"
In a console, how to create files (no mouse to right click and select “create new file” 🙂 ). Use the touch command
ls command can tell a lot about file and folders e.g. if file/folder permission information is require then use
It show user, group, date and time of creation and type of permission on a file and folder. How to understand permission information e.g
drwxrwxr-x Tony:stark ...............
Lets break it down
directory owner group others d rwx rwx r-x
it shows owner, group and others permission information e.g. the above item is a folder and owner “Tony” has read, write and execute permissions. user belong to group “stark” have read, write and execute on this item and all other users have read and execute permissions but they cannot write to this item.
What if a user wants to have permissions to read, write or execute. Here comes change mode command
chmod a=rwx file/foler
chmod a=rwx Iron-man.txt
sudo is required.
If item is a folder and contains sub-folders and files, the recursive switch can help to set the same permissions for files/sub-folders inside the parent folder
chmod -R a=rwx file/folder
a=rwx means that all owner, group and others will have read, write and execution permission.
If just one type of user is to be given permissions then use the following versions
Give owner read and write permissions chmod u+rw file/folder
Give group read and execute permissions chmod g+rx file/folder
Give others read and execute permissions chmod o+rw file/folders
or combine user and group in one command
chmod u+r,g+x file/folder
Similarly, permission can be revoked using change mode command e.g. disallow others the permissions of read and write
chmod o-rw file/folder
Find a string in file (in specific folder)
grep -nr yourString* .-r will enable grep to search in sub folders
Remove files and folders recursively
rm -rf foldername