The command line is a powerful tool in any developer’s arsenal. It makes operational tasks faster and affords developers more control over their workflow without needing to rely on a collection of specialized apps or tools that accomplish the same results. However, many front-end developers feel lost when it comes to the using the command line. They may know commands related to specific tools like Git, NPM, or Gulp/Grunt, but there is so much more to harness for the sake of efficiency and greater productivity.
In this article, I will seek to rid front-end developers of this ill-found insecurity, by equipping them with a few useful commands that they can employ on their next web project.
- Compress Or Extract Files Using tar
- Backup A Database (MySQL Dump)
- Search For Files Within A Project
- Transfer Files Between Remote And Local Environments
Compress Or Extract Files Using tar
Compressing files before transferring or archiving them can save you time and bandwidth. The following examples use the
tar command to compress/extract archives using gzip. This is still one of the best ways to compress a collection of files into an single archive file.
This command uses the
-c flag to signal we want to compress files. Replace
archive.tar.gz with your desired file name and
/path/to/directory-or-file with the directory you want to compress.
tar -czvf archive.tar.gz /path/to/directory-or-file
This command uses the
-x flag to signal we want to extract files. The
-C flag signals that
tar should change directories to
tmp (or whatever directory you choose) and extract the files there instead of the current directory.
tar -xzvf archive.tar.gz -C tmp
Backup A Database (MySQL Dump)
Sure, this can easily be done via most server management platforms, but sometimes it’s just easier to pull a database backup from the command line. The beauty in this command is it’s simplify. It requires the database username and password, but that’s it. If you want to be a super user, you can even combine this command with the previous
tar example to get a database snapshot along with the files, all in one archive file.
mysqldump -u username -p existing_db_name > existing_db_name.sql
Search For Files Within A Project
Searching for a particular file or text string on a remote server is a pain in the neck with most server management tools, but not with these commands. Get ready to find files faster than you ever thought possible.
Search For Files By Filename
This command will return a list of files matching your query within a directory. You can use
* in any part of the target directory as a wildcard to search all directories. For example,
/home/*/public_html/ would search the
/public_html directory within any child directories in
find /path/to/search -name filename.ext
Search For Files That Contain A Text String
In contrast to the previous example, this command searches the contents of files within a directory. The following command will produce a list of files where the search string is present within the file contents. The
-r flag tells
grep to search recursively within all sub-directories of
-l flag tells
grep to list the files where a match is found.
grep -rl 'search string' /path/to/search
Transfer Files Between Environments
This is basically an FTP replacement command. You connect to the remote server via SSH and transfer files between local and remote all in one command. Remember that
tar command from earlier? This is a good follow up to download that archive file. The
-P flag allows you to specify the SSH port. It you want to use this command to transfer directories and all their contents, use the
-r flag to copy directories recursively.
Copy Files From Remote To Local :
scp -P 22 firstname.lastname@example.org:/path/to/remote/file /path/to/local/file
Copy Files From Local To Remote:
scp -P 22 /path/to/local/file email@example.com:/path/to/remote/file
Don’t Stop Here, Keep Going
Now you have a few more tools at your disposal to make you a more efficient developer. I challenge you to keep digging into all the options for these commands and others. Find the ones that work best for you and pull into your workflow.