Basic Linux commands and absolute paths for your hosting
On a hosting account, a terminal lets you handle your files with typed commands instead of clicks. A handful of commands and a clear idea of what a path is are enough to find your way around.
Before you start
- You need command-line access to your hosting: see Log in to your hosting over SSH. Not every hosting plan opens this access: cPanel states that "not all hosting providers allow shell access".
- The commands below are standard Linux commands (GNU Coreutils), the same on most servers. Replace
usernamewith your cPanel username. - Nothing can be undone on the command line: a deletion does not send the file to a trash folder. Make a backup before changing or deleting many files.
Understand paths
A path says where a file or folder is.
- An absolute path starts with
/(the root of the system) and describes the whole route. It always points to the same place, wherever you type it. Example:/home/username/public_html/index.html. - A relative path does not start with
/: it starts from the folder you are in. If you are in/home/username,public_html/index.htmlpoints to the same file. .is the current folder,.its parent folder, and~your home folder.
On a cPanel hosting account, the cPanel documentation describes an account like this:
- your home directory is the account's main folder,
/home/username; - the web root is
public_html, that is/home/username/public_html: your website's files live there; - an additional FTP account is limited to a subfolder of
/home/username/(see Create additional FTP accounts).
Absolute paths matter in scripts, scheduled tasks and configuration files: a program started automatically is not in the folder you think it is.
Steps
Open your session (see the article on SSH login). Type each line below, then press Enter.
- Find out where you are:
pwdprints the absolute path of the current folder. - List a folder's content:
ls. Two useful options:ls -l(one line per item, with permissions, size and date) andls -a(also shows hidden files, whose name starts with a dot, like.htaccess). Combine them:ls -la. - Change folder:
cd public_html(relative path) orcd /home/username/public_html(absolute path).cd .goes up one level;cdalone (orcd ~) takes you back to your home folder. - Create a folder:
mkdir new-folder. - Create an empty file:
touch file.txt. - Copy:
cp source.txt copy.txt. For a folder and its content, add-r:cp -r folder folder-copy. The-ioption asks before overwriting a file. - Move or rename:
mv old.txt new.txtrenames;mv file.txt folder/moves. - Read a file:
cat file.txtprints all of it;tail -n 50 file.logprints only its last 50 lines (handy for an error log). - Delete:
rm file.txtdeletes a file;rm -r folderdeletes a folder and everything in it. The-ioption asks for confirmation for each item. - Check sizes:
du -sh folderprints a folder's total size in a readable unit (-s: one total;-h: KB, MB, GB).du -sh *gives the size of each item in the current folder. - Change a file's permissions:
chmod 644 file.txtsets the permissions as a number. Each digit stands, in order, for the owner, the group and others. By default, cPanel's File Manager uses0644for a file.
Expected result
You can tell where you are (pwd), move around your folders (ls, cd), create, copy, move and delete items, measure what takes up space (du -sh) and write the absolute path of a file, for example /home/username/public_html/index.html.
If something goes wrong
- "No such file or directory": the path is wrong. Check the spelling (Linux is case sensitive) and the current folder with
pwd. - "Permission denied": your account may not read or write there. Stay in your home folder (
/home/username): your files are yours there. - "command not found": the command is misspelled, or not available in your environment. On shared hosting, access is sometimes restricted to a limited ("jailed") shell, and some commands may be missing.
- A file does not show in
ls: its name may start with a dot. Usels -a. - A site stops working after a
chmod: put the usual permissions back (0644for a file) or restore the backup.
FAQ
What is the difference between an absolute and a relative path?
An absolute path starts with / and begins at the root of the system (/home/username/public_html). A relative path starts from the folder you are in. In a script or a scheduled task, prefer the absolute path.
What is my site's absolute path on cPanel hosting?
According to the cPanel documentation, it is the public_html web root in your home directory: /home/username/public_html, where username is your cPanel username. An additional domain can have another folder: cPanel's Domains page shows each domain's document root.
Can I recover a file deleted with rm?
Not from a trash folder: rm does not use one. If a backup exists, restore it. In cPanel's File Manager, however, deleting first moves the item to the Trash folder (see Use cPanel's File Manager).
Do I need to learn the terminal to manage my site?
No. cPanel's File Manager covers uploading, copying, editing and deleting files from your browser. The terminal helps to automate, to go faster on large volumes or to read logs.
Related articles
- Log in to your hosting over SSH (key or password)
- Use cPanel's File Manager
- Disk space full in cPanel: find what takes up space
- Back up your site, emails and databases in cPanel
Need full access to your server? Discover our virtual private servers (VPS).