Basic Unix Commands
If you’re just getting started with the the basic Unix commands there are a few key prompts that you’ll use again and again. These take a bit of getting used to but can hugely speed up the process of doing things like setting up wordpress, looking at error logs, and general day to day running of a server. All you’ll need is a web host that gives you ssh access and a bit of patience!
Connecting to a Server:
On windows you’ll want to run cmd.exe and if you’re on a Mac, open up Terminal (use spotlight if you can’t find it). Once that’s done just type:
ssh yourUserName@yoursite.com
You’ll then probably see an alert asking you if you want to add the server’s RSA key… just type yes and hit return. All you’ll need next is your password – type it in, hit return again and you’re done. The connection to your server is now open, meaning you have access to all of the files and folders.
Moving around your directories:
These will probably be your most used unix commands as they allow you to navigate around your file structure. This is just like clicking on icons, except you’re doing so by typing in the location that you’re trying to get to.
pwd
– Show the current directory (“present working directory”).cd /directory/to/change/to
– takes you to the specified directory.cd subdirectory/of/current/location
– missing the first / means that you go to a subdirectory. cd .. – Change current directory to the parent directory of the current directory.
Listing directory contents:
Again, these commands will be used a huge amount. Until you can remember the structure of your files and folders you’ll end up changing directories, listing the contents, and then navigating. It’s a little slow going at first but once you get used to it you’ll fly around
ls
– show any files and subdirectories in your current location.ls -l
– as above but with more detailed information.ls -lh
– detailed information in a more readable format.ls -a
– includes hidden files (can be used in conjunction with the other commands – ls -lha).
Changing file and folder permissions:
Particularly useful when working with WordPress sites, log files, etc. One of the most common reasons for changing folder permissions is when a script is trying to write to a file (ie. uploading an image to the uploads folder) but isn’t able to do so. In that instance you’d use -R and set the permissions to 777.
chmod 755 file-name
– change the file permissions of the specified file.chmod -r 777 uploads
– allow writing of the uploads folder and any files or folders inside it.chown matt file-name
– Makes user matt the owner of the specified file.chown -R matt dir-name
– Makes matt the owner of the directory and its contents.
Remember, you must have write permissions for the file or folder that you’re trying to change – if not, these unix commands won’t work.
Copying, moving and renaming files or folders:
Working with files is really simple – there are 4 basic commands for creating, copying, moving and removing files or folders. Think of it as cut, copy and paste but faster and
more powerful.
touch file1
– create file1 in the current directory.cp file1.ext file2.ext
– copy file1 and create a duplicate as file2.mv file1.ext newname.ext
– rename or move file1 to newname.mv file1 ..
– move file1 into the parent directory of the one you’re currently in.mv file1 /full/path/of/folder
– move a file to the specified folder.rm file1
– delete file1rm -r dir1
– delete the specified directory and ALL of it’s files and folders.rm -fr dir1
– the same as above, but doesn’t ask you to confirm deletions. Be careful with this onemkdir dir1
– create dir1 as a subfolder of the current path.
Viewing a text file
There’s huge debate about which the best text editor is but I personally like nano. It’s really say to work with and gives you the ability to navigate through and change text files really quickly. ‘tail’ is also really useful, particularly when viewing log files.
nano filename.ext
– open filename using nano and view its contents.tail -100 filename.ext
– view the last 100 lines of the specified file.head -100 filename.ext
– like tail, but shows the first 100 lines of the specified file.grep string filename
– searches a file for a given string and outputs the matched results.
Searching for files
There’ll be times when you’re trying to find a certain log file or hunting for a script that you’ve moved somewhere safe. Again, this is really fast and easy.
find . -dirname logfile.log
– searches the current folder and all of its subfolders for logfile.log.find / -name lostfile
– searches for all files named ‘lostfile’ anywhere on your system.find /sub/directory -name "*wildcard*"
– searches /sub/directory for all files that contain the string ‘wildcard’ anywhere in their name.
I need help
Finally, if you’ve forgotten how to do something, or just want to find out more you can use ‘man’. This brings up the manual for any given command:
man ls
Hopefully that’s enough to get started with basic unix commands – just learning these will easily give you the ability to navigate your system and play around with files and folders. There’s not too much that can go wrong – just be careful when moving or deleting entire folders… One they’re gone, that’s it – no turning back